-
Notifications
You must be signed in to change notification settings - Fork 0
/
prescan_healthcheck.rb
107 lines (90 loc) · 2.31 KB
/
prescan_healthcheck.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env ruby
require 'rubygems'
require 'nexpose'
require 'io/console'
#require 'pp'
require 'Time'
#vars
host = 'FQDN OR IP HERE'
port = '3780'
user = 'PUT YOUR USERNAME HERE'
pass = 'PASSWORD HERE'
#Create connector
puts "Nexpose Pre-Scan Health Check Script: v1.0 20140310"
puts "\nConnecting to #{host} as #{user}..."
nsc = Nexpose::Connection.new(host, user, pass, port)
begin
#auth
nsc.login
#clean logout
at_exit { nsc.logout }
rescue ::Nexpose::APIError => err
$stderr.puts("Connection failed: #{e.reason}")
exit(1)
end
#Check system info
sysinfo = nsc.system_information
#pp sysinfo
#One blink for yes, two blinks for no!
class Beep
#The use of "self" init a class method rather than instance method
def self.pass
print "\a"
end
def self.fail
print "\a \a"
end
end
begin
#Check for the last update
NSCLastUpdate = sysinfo["last-update-date"]
CTime = Time.now.to_i
#Check to see if the update was within last 7 days
if (CTime + 604800) < NSCLastUpdate.to_i
puts "Last Update: OK"
elsif (CTime + 604800) >= NSCLastUpdate.to_i
puts "Last Update: Not Updated within 7 days"
puts Time.at(CTime)
Beep.fail
puts "Starting update. Please wait."
NSCUpdate = nsc.console_command("updatenow")
puts NSCUpdate
puts "Pushing update to scan engines. Please wait."
EngineUpdate = nsc.console_command("update engines")
end
rescue StandardError => UpdateError
print UpdateError
end
begin
#Check memory utilization
NSCFreeMem = sysinfo["ram-free"]
NSCTotalMem = sysinfo["ram-total"]
NSCMemUse = (NSCFreeMem.to_i / NSCTotalMem.to_i)
#Check to see if we are at 75% or greater usage
if NSCMemUse < (0.75)
puts "Memory Usage: OK"
elsif NSCMemUse >= (0.75)
puts "Memory Usage: Above 75%"
puts "Utilization:" + NSCMemUse.to_s
Beep.fail
puts "Attempting to free up Java resources. Please wait."
GarbageCollect = nsc.console_command("garbagecollect")
puts GarbageCollect
end
rescue StandardError => MemError
print MemError
end
begin
#Check up time
NSCUpTime = sysinfo["up-time"]
NSCUpTimeThreshold = 600
#Check to see if up time is greater than 5 minutes
if NSCUpTime.to_i >= NSCUpTimeThreshold
puts "Uptime: OK"
elsif NSCUpTime.to_i <= NSCUpTimeThreshold
puts "Uptime: Recent service restart. Potential Issue."
Beep.fail
end
rescue StandardError => UptimeError
print UptimeError
end