gnms 2.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gnms might be problematic. Click here for more details.

Files changed (140) hide show
  1. data/LICENSE +459 -0
  2. data/README +29 -0
  3. data/bin/gnms +11 -0
  4. data/gnms.gemspec +70 -0
  5. data/lib/cmd_parse.rb +747 -0
  6. data/lib/config_global.rb +414 -0
  7. data/lib/contact.rb +171 -0
  8. data/lib/external/crypt/blowfish-tables.rb +190 -0
  9. data/lib/external/crypt/blowfish.rb +110 -0
  10. data/lib/external/crypt/cbc.rb +123 -0
  11. data/lib/external/crypt/stringxor.rb +27 -0
  12. data/lib/external/fog.rb +41 -0
  13. data/lib/external/jjmx.rb +45 -0
  14. data/lib/external/sms.rb +282 -0
  15. data/lib/external/xmpp4r-simple.rb +497 -0
  16. data/lib/format.rb +85 -0
  17. data/lib/gnms.rb +329 -0
  18. data/lib/gui/about_window.rb +177 -0
  19. data/lib/gui/adddelnode.rb +799 -0
  20. data/lib/gui/base_window.rb +789 -0
  21. data/lib/gui/canvas_map.rb +498 -0
  22. data/lib/gui/charts/gruff.rb +51 -0
  23. data/lib/gui/color_macro.rb +145 -0
  24. data/lib/gui/config_window.rb +2151 -0
  25. data/lib/gui/custom_plugin_window.rb +90 -0
  26. data/lib/gui/debug_window.rb +46 -0
  27. data/lib/gui/event_window.rb +469 -0
  28. data/lib/gui/find_node_window.rb +438 -0
  29. data/lib/gui/help_window.rb +47 -0
  30. data/lib/gui/ipcalculator.rb +147 -0
  31. data/lib/gui/jmx_ref_window.rb +309 -0
  32. data/lib/gui/link_view.rb +46 -0
  33. data/lib/gui/mib_browser.rb +585 -0
  34. data/lib/gui/node_host_property.rb +3712 -0
  35. data/lib/gui/node_host_view.rb +270 -0
  36. data/lib/gui/node_network_property.rb +1059 -0
  37. data/lib/gui/node_network_view.rb +186 -0
  38. data/lib/gui/node_view.rb +672 -0
  39. data/lib/gui/oid_ref_window.rb +288 -0
  40. data/lib/gui/snmptrap_ref_window.rb +288 -0
  41. data/lib/gui/sub/account_frame.rb +196 -0
  42. data/lib/gui/sub/contact_frame.rb +519 -0
  43. data/lib/gui/syslog_ref_window.rb +288 -0
  44. data/lib/gui/treenode_window.rb +320 -0
  45. data/lib/gui/update_window.rb +124 -0
  46. data/lib/gui/widgets.rb +169 -0
  47. data/lib/gui/wmi_ref_window.rb +286 -0
  48. data/lib/interface.rb +16 -0
  49. data/lib/ipcalc.rb +266 -0
  50. data/lib/link.rb +45 -0
  51. data/lib/main.rb +1091 -0
  52. data/lib/monitor/client/custom/custom_monitoring.rb +40 -0
  53. data/lib/monitor/client/jmx/jmx_attr_monitoring.rb +36 -0
  54. data/lib/monitor/client/jmx/jmx_monitoring.rb +83 -0
  55. data/lib/monitor/client/jmx/jmx_ref.rb +114 -0
  56. data/lib/monitor/client/snmp/mibs/SNMPv2-MIB.txt +854 -0
  57. data/lib/monitor/client/snmp/mibs/SNMPv2-SMI.txt +344 -0
  58. data/lib/monitor/client/snmp/oid_ref.rb +141 -0
  59. data/lib/monitor/client/snmp/snmp.rb +178 -0
  60. data/lib/monitor/client/snmp/snmp_monitoring.rb +58 -0
  61. data/lib/monitor/client/wmi/wmi.rb +139 -0
  62. data/lib/monitor/client/wmi/wmi_monitoring.rb +60 -0
  63. data/lib/monitor/client/wmi/wmi_ref.rb +141 -0
  64. data/lib/monitor/custom_plugin.rb +50 -0
  65. data/lib/monitor/server/msg_buffer.rb +39 -0
  66. data/lib/monitor/server/snmp/snmptrap_analyzer.rb +81 -0
  67. data/lib/monitor/server/snmp/snmptrap_capture.rb +26 -0
  68. data/lib/monitor/server/snmp/snmptrap_monitoring.rb +32 -0
  69. data/lib/monitor/server/snmp/snmptrap_ref.rb +135 -0
  70. data/lib/monitor/server/snmp/snmptrap_server.rb +50 -0
  71. data/lib/monitor/server/syslog/syslog_analyzer.rb +141 -0
  72. data/lib/monitor/server/syslog/syslog_capture.rb +28 -0
  73. data/lib/monitor/server/syslog/syslog_monitoring.rb +32 -0
  74. data/lib/monitor/server/syslog/syslog_ref.rb +136 -0
  75. data/lib/monitor/server/syslog/syslog_server.rb +43 -0
  76. data/lib/node.rb +1020 -0
  77. data/lib/node_db.rb +2668 -0
  78. data/lib/node_host.rb +215 -0
  79. data/lib/node_listener.rb +639 -0
  80. data/lib/node_network.rb +269 -0
  81. data/lib/node_property.rb +39 -0
  82. data/lib/notify/alarm.rb +247 -0
  83. data/lib/notify/irc_bot.rb +167 -0
  84. data/lib/notify/xmpp_bot.rb +107 -0
  85. data/lib/service.rb +36 -0
  86. data/lib/version.rb +8 -0
  87. data/pixmap/bg/map_bg.jpg +0 -0
  88. data/pixmap/bg/map_bg2.jpg +0 -0
  89. data/pixmap/logo.jpg +0 -0
  90. data/pixmap/logo_icon.png +0 -0
  91. data/pixmap/os/3com.png +0 -0
  92. data/pixmap/os/aix.gif +0 -0
  93. data/pixmap/os/amiga.png +0 -0
  94. data/pixmap/os/beos.png +0 -0
  95. data/pixmap/os/cisco.png +0 -0
  96. data/pixmap/os/freebsd.png +0 -0
  97. data/pixmap/os/hp.png +0 -0
  98. data/pixmap/os/hurd.gif +0 -0
  99. data/pixmap/os/irix.gif +0 -0
  100. data/pixmap/os/linux.png +0 -0
  101. data/pixmap/os/mac.png +0 -0
  102. data/pixmap/os/netbsd.gif +0 -0
  103. data/pixmap/os/netware.png +0 -0
  104. data/pixmap/os/openbsd.png +0 -0
  105. data/pixmap/os/os2warp.gif +0 -0
  106. data/pixmap/os/qnx.gif +0 -0
  107. data/pixmap/os/sco.gif +0 -0
  108. data/pixmap/os/sgi.xpm +111 -0
  109. data/pixmap/os/solaris.gif +0 -0
  110. data/pixmap/os/unix.gif +0 -0
  111. data/pixmap/os/unknown.png +0 -0
  112. data/pixmap/os/vms.xpm +345 -0
  113. data/pixmap/os/windows.png +0 -0
  114. data/pixmap/type/cloud.png +0 -0
  115. data/pixmap/type/device.png +0 -0
  116. data/pixmap/type/firewall.png +0 -0
  117. data/pixmap/type/gateway.png +0 -0
  118. data/pixmap/type/home.png +0 -0
  119. data/pixmap/type/host.png +0 -0
  120. data/pixmap/type/network.png +0 -0
  121. data/pixmap/type/printer.png +0 -0
  122. data/pixmap/type/router.png +0 -0
  123. data/pixmap/type/server.png +0 -0
  124. data/pixmap/type/switch.png +0 -0
  125. data/pixmap/type/vm.png +0 -0
  126. data/plugins/Defaultme.rb +7 -0
  127. data/plugins/DnsCheck.rb +46 -0
  128. data/plugins/FooMonitor.rb +26 -0
  129. data/plugins/HttpVirtualHostMonitor.rb +43 -0
  130. data/plugins/MysqlShowStatusMonitor.rb +64 -0
  131. data/plugins/PgsqlPercentConnectionMonitor.rb +72 -0
  132. data/plugins/SshCpuLoadMonitor.rb +85 -0
  133. data/plugins/SshDiskMonitor.rb +69 -0
  134. data/plugins/SshFgtAvExpiredMonitor.rb +75 -0
  135. data/plugins/SshPercentUsedRamMonitor.rb +74 -0
  136. data/plugins/TelnetCpuLoadMonitor.rb +69 -0
  137. data/plugins/example1.rb +15 -0
  138. data/plugins/example2.rb +7 -0
  139. data/sound/beep.wav +0 -0
  140. metadata +378 -0
@@ -0,0 +1,16 @@
1
+ class Interface
2
+
3
+ attr_accessor :name, :link_status, :speed, :type
4
+
5
+ def initialize (n, lstatus=nil, sp=nil, tp=nil)
6
+ @name = n
7
+ @link_status = lstatus
8
+ @speed = sp
9
+ @type = tp
10
+ end
11
+
12
+ def to_s()
13
+ print "Interface: ", @name, " ", @link_status, " ", @speed, " ", @type, "\n"
14
+ end
15
+
16
+ end #class
@@ -0,0 +1,266 @@
1
+ module IPCalc
2
+
3
+ def IPCalc.bits_to_netmask(bits)
4
+ bits=bits.to_i()
5
+ if bits <=0
6
+ return "0.0.0.0"
7
+ end
8
+ if bits >= 32
9
+ return "255.255.255.255"
10
+ end
11
+
12
+ cpt=0
13
+ cpt_byte=8
14
+ result=""
15
+ result_val=0
16
+ while bits >= 0 ||cpt <= 32
17
+ val=1
18
+ val=0 unless bits > 0
19
+ if cpt%8==0
20
+ if cpt != 0
21
+ result+="#{result_val}"
22
+ result+="." unless cpt == 32
23
+ cpt_byte=8
24
+ result_val=0
25
+ end
26
+ end
27
+ result_val+=2**(cpt_byte-1)*val
28
+ cpt_byte-=1
29
+ cpt+=1
30
+ bits-=1
31
+ end
32
+ return result
33
+ end
34
+
35
+ def IPCalc.netmask_to_bits(netmask)
36
+ #verify if netmask is an 4 dot format
37
+ if netmask.nil? or netmask.empty? or !isValidIPv4(netmask)
38
+ return nil
39
+ end
40
+ netmask_t=netmask.split(/\./)
41
+ result_val=0
42
+
43
+ for val in netmask_t
44
+ cpt_byte=8
45
+ val_tmp=val.to_i()
46
+ while val_tmp > 0
47
+ val_tmp-=2**(cpt_byte-1)
48
+ cpt_byte-=1
49
+ result_val+=1
50
+ end
51
+ end
52
+ return result_val
53
+ end
54
+
55
+
56
+ #
57
+ # take an integer
58
+ #
59
+ def IPCalc.get_wildcard(mask)
60
+ mask_local=mask
61
+ if mask.to_s().match('^\d+$')
62
+ mask_local=bits_to_netmask(mask)
63
+ end
64
+ #verify if mask_local is an 4 dot format
65
+ if !isValidIPv4(mask_local)
66
+ return nil
67
+ end
68
+ netmask_t=mask_local.split(/\./)
69
+ result=""
70
+ for val in netmask_t
71
+ val_tmp=val.to_i()
72
+ result+="#{255-val_tmp}."
73
+ end
74
+ return result.chop
75
+ end
76
+
77
+ def IPCalc.get_network_and_mask(addr)
78
+ #verify if addr is an 4 dot format string
79
+ if !isValidIPv4(addr)
80
+ return nil
81
+ end
82
+ result_t=[]
83
+ mask=class_type_default_mask(addr)
84
+ wildcard=get_wildcard(mask)
85
+ wildcard_t=wildcard.split(/\./)
86
+ addr_t=addr.split(/\./)
87
+ cpt=0
88
+ for wildc in wildcard_t
89
+ if wildc != "0"
90
+ addr_t[cpt]="0"
91
+ end
92
+ cpt+=1
93
+ end
94
+ result=addr_t.join(".")
95
+ if result == addr
96
+ return nil
97
+ else
98
+ result_t.push result
99
+ result_t.push mask
100
+ return result_t
101
+ end
102
+ end
103
+
104
+ def IPCalc.get_network_addr(addr, mask)
105
+ #verify if addr is an 4 dot format string
106
+ if !isValidIPv4(addr)
107
+ return nil
108
+ end
109
+ wildcard=get_wildcard(mask)
110
+ wildcard_t=wildcard.split(/\./)
111
+ addr_t=addr.split(/\./)
112
+ cpt=0
113
+ for wildc in wildcard_t
114
+ if wildc != "0"
115
+ addr_t[cpt]="0"
116
+ end
117
+ cpt+=1
118
+ end
119
+ return addr_t.join(".")
120
+ end
121
+
122
+ def IPCalc.get_broadcast(network, mask)
123
+ #verify if network is an 4 dot format string
124
+ if !isValidIPv4(network)
125
+ return nil
126
+ end
127
+ wildcard=get_wildcard(mask)
128
+ wildcard_t=wildcard.split(/\./)
129
+ addr_t=network.split(/\./)
130
+ cpt=0
131
+ for wildc in wildcard_t
132
+ if wildc=="0"
133
+ wildcard_t[cpt]=addr_t[cpt] unless addr_t[cpt] == "0"
134
+ end
135
+ cpt+=1
136
+ end
137
+ return wildcard_t.join(".")
138
+
139
+ end
140
+
141
+ def IPCalc.nb_host(netmask)
142
+ mask=netmask
143
+ if !netmask.to_s().match('^\d+$')
144
+ mask=netmask_to_bits(netmask)
145
+ end
146
+ if mask==nil
147
+ return nil
148
+ end
149
+ mask=mask.to_i()
150
+ result=2**(32-mask)-2
151
+ result=1 if result<=0
152
+ return result
153
+ end
154
+
155
+ def IPCalc.to_bitwise(four_dot_addr)
156
+ #verify if four_dot_addr is an 4 dot format string
157
+ if !isValidIPv4(four_dot_addr)
158
+ return nil
159
+ end
160
+ four_dot_addr_t=four_dot_addr.split(/\./)
161
+ result=""
162
+ for four_dot_split in four_dot_addr_t
163
+ byte_val=8
164
+ while four_dot_split.to_i>0
165
+ if four_dot_split.to_i >= 2**(byte_val-1)
166
+ result+="1"
167
+ four_dot_split=four_dot_split.to_i()-2**(byte_val-1)
168
+ else
169
+ result+="0"
170
+ end
171
+ byte_val-=1
172
+ end
173
+ result+="0"*byte_val
174
+ result+="."
175
+ end
176
+ return result.chop
177
+ end
178
+
179
+ def IPCalc.class_type_name(network)
180
+ class_name="class"
181
+ mask=network
182
+ if network.to_s().match('^\d+$')
183
+ mask=bits_to_netmask(network)
184
+ end
185
+ if mask==nil
186
+ return nil
187
+ end
188
+ bitmask=to_bitwise(mask)
189
+ if bitmask.to_s().match('^0')
190
+ return class_name+" "+"A"
191
+ elsif bitmask.to_s().match('^10')
192
+ return class_name+" "+"B"
193
+ elsif bitmask.to_s().match('^110')
194
+ return class_name+" "+"C"
195
+ elsif bitmask.to_s().match('^1110')
196
+ return class_name+" "+"D"
197
+ elsif bitmask.to_s().match('^1111')
198
+ return class_name+" "+"E"
199
+ end
200
+ end
201
+
202
+ def IPCalc.class_type_default_mask(addr)
203
+ #verify if addr is an 4 dot format string
204
+ if !isValidIPv4(addr)
205
+ return nil
206
+ end
207
+ class_name="class"
208
+ mask=addr
209
+ if addr.to_s().match('^\d+$')
210
+ mask=bits_to_netmask(netmask)
211
+ end
212
+
213
+ bitmask=to_bitwise(mask)
214
+ if bitmask.to_s().match('^0')
215
+ return 8
216
+ elsif bitmask.to_s().match('^10')
217
+ return 16
218
+ elsif bitmask.to_s().match('^110')
219
+ return 24
220
+ elsif bitmask.to_s().match('^1110')
221
+ return 4
222
+ elsif bitmask.to_s().match('^1111')
223
+ return 4
224
+ end
225
+ end
226
+
227
+ #
228
+ # launch ipcalculator ip can be a 4 dot addr or addr/bitmask
229
+ #
230
+ def IPCalc.ipcalc_detail(addr_str)
231
+ addr=""
232
+ mask=""
233
+ if isValidIPv4(addr_str)
234
+ #we need default mask
235
+ addr=addr_str
236
+ mask=class_type_default_mask(addr_str)
237
+ elsif addr_str.match('^[\d.]+\/\d+$')
238
+ addr,mask=addr_str.split(/\//)
239
+ end
240
+ result=
241
+ "Address: "+addr+"\n"+
242
+ "NetMask: "+mask+"\n"+
243
+ "Wildcard: "+get_wildcard(mask)+"\n"+
244
+ "Network: "+get_network_addr(addr,mask)+"\n"+
245
+ "Broadcast: "+get_broadcast(addr,mask)+"\n"+
246
+ "Hosts: "+nb_host(mask).to_s()+"\n"+
247
+ "Type: "+class_type_name(addr)+"\n\n";
248
+
249
+ #HostMin:
250
+ #HostMax:
251
+ return result
252
+ end
253
+
254
+ #puts bits_to_netmask(ARGV[0].to_i)
255
+ #puts netmask_to_bits("0.0.0.0")
256
+ #puts get_wildcard("255.255.252.0")
257
+ #puts get_wildcard(22)
258
+ #puts get_broadcast("12.168.1.0",22)
259
+ #puts get_network_addr("12.168.1.2",22)
260
+ #puts nb_host(22)
261
+ #puts to_bitwise("192.168.1.1")
262
+ #puts class_type_name("18.0.0.0")
263
+ #puts get_network_and_mask("10.10.1.1")
264
+
265
+ #to do : hostmin hostmax priv/pub
266
+ end
@@ -0,0 +1,45 @@
1
+ require "#{GNMSLIB}"+'/gui/link_view'
2
+
3
+ class Link
4
+ attr_accessor :node1, :node2
5
+
6
+ def initialize(n1, n2)
7
+ #true for up, false for down
8
+ @state=true
9
+ @name=nil
10
+ @speed=nil
11
+ @node1=n1
12
+ @node2=n2
13
+ $links.push(self)
14
+ @node1.add_link(self)
15
+ @node2.add_link(self)
16
+ @view=LinkView.new(n1.map, self, [@node1.status, @node2.status].max)
17
+ end
18
+
19
+ def point?(node)
20
+ if (node==@node1) || (node==@node2)
21
+ return true
22
+ end
23
+ return false
24
+ end
25
+
26
+ def show()
27
+ @view.show()
28
+ end
29
+
30
+ def hide()
31
+ @view.hide()
32
+ end
33
+
34
+ def display()
35
+ @view.display()
36
+ end
37
+
38
+ def remove_link()
39
+ @view.remove_link()
40
+ end
41
+
42
+ def update_status()
43
+ @view.update_status([@node1.status, @node2.status].max)
44
+ end
45
+ end #class
@@ -0,0 +1,1091 @@
1
+ require 'gdk_pixbuf2'
2
+
3
+ begin
4
+ require 'rubygems'
5
+ $defined_lib_gruff=true
6
+ rescue LoadError
7
+ $log.error("rubygems library is missing thus gruff library for report will not be loaded")
8
+ $defined_lib_gruff=false
9
+ end
10
+
11
+ begin
12
+ require 'goocanvas'
13
+ rescue LoadError
14
+ $log.fatal("You must install goocanvas module for ruby")
15
+ exit()
16
+ end
17
+
18
+ begin
19
+ require 'facter'
20
+ require 'facter/util/ip'
21
+ rescue LoadError
22
+ $log.fatal("You must install facter lib for ruby to be able to extract system info")
23
+ exit()
24
+ end
25
+
26
+ require 'securerandom'
27
+ require 'syslog'
28
+ require 'tempfile'
29
+ require 'optparse'
30
+ require 'base64'
31
+
32
+ begin
33
+ require 'snmp'
34
+ rescue LoadError
35
+ $log.fatal("You must install snmp module for ruby from http://snmplib.rubyforge.org/")
36
+ exit()
37
+ end
38
+
39
+ begin
40
+ require 'sqlite3'
41
+ if !SQLite3::VERSION.match("^1\.3")
42
+ raise LoadError
43
+ end
44
+ rescue LoadError
45
+ $log.fatal("You must install SQLite v3 module for ruby from http://rubyforge.org/projects/sqlite-ruby")
46
+ exit()
47
+ end
48
+
49
+ begin
50
+ require 'gst'
51
+ Gst.init([])
52
+ $defined_lib_gst=true
53
+ rescue LoadError
54
+ $log.error("You will not be able to play sound files, you need ruby lib gstreamer (libgst)")
55
+ $defined_lib_gst=false
56
+ end
57
+
58
+ require "#{GNMSLIB}"+'/monitor/client/snmp/oid_ref'
59
+ require "#{GNMSLIB}"+'/monitor/client/wmi/wmi_ref'
60
+ require "#{GNMSLIB}"+'/monitor/client/jmx/jmx_ref'
61
+ require "#{GNMSLIB}"+'/monitor/server/syslog/syslog_ref'
62
+ require "#{GNMSLIB}"+'/monitor/server/snmp/snmptrap_ref'
63
+
64
+ require "#{GNMSLIB}"+'/node'
65
+ require "#{GNMSLIB}"+'/node_host'
66
+ require "#{GNMSLIB}"+'/node_network'
67
+ require "#{GNMSLIB}"+'/link'
68
+ require "#{GNMSLIB}"+'/gui/event_window'
69
+ require "#{GNMSLIB}"+'/gui/canvas_map'
70
+ require "#{GNMSLIB}"+'/monitor/server/syslog/syslog_capture'
71
+ require "#{GNMSLIB}"+'/monitor/server/snmp/snmptrap_capture'
72
+ require "#{GNMSLIB}"+'/format'
73
+ require "#{GNMSLIB}"+'/contact'
74
+ require "#{GNMSLIB}"+'/external/crypt/blowfish'
75
+ require "#{GNMSLIB}"+'/external/fog'
76
+
77
+ #SMS lib from http://raa.ruby-lang.org/project/ruby-sms/
78
+ #need serialport lib too http://raa.ruby-lang.org/project/ruby-serialport/
79
+
80
+ begin
81
+ require "#{GNMSLIB}"+'/external/sms.rb'
82
+ rescue LoadError
83
+ $log.error("SMS notification disabled, library ruby-serialport is missing")
84
+ $defined_lib_sms=false
85
+ end
86
+
87
+ ################################################################################
88
+ # Main Event Window
89
+ ################################################################################
90
+
91
+
92
+ #
93
+ # return a version 4 UUID
94
+ # it's std lib in ruby 1.9
95
+ #
96
+ def create_uuid()
97
+ if (defined?(SecureRandom.uuid))
98
+ SecureRandom.uuid
99
+ else
100
+ ary = SecureRandom.random_bytes(16).unpack("NnnnnN")
101
+ ary[2] = (ary[2] & 0x0fff) | 0x4000
102
+ ary[3] = (ary[3] & 0x3fff) | 0x8000
103
+ "%08x-%04x-%04x-%04x-%04x%08x" % ary
104
+ end
105
+ end
106
+
107
+ def blowfish_encode(str)
108
+ begin
109
+ blowfish = Crypt::Blowfish.new(GNMSPPH)
110
+ return blowfish.encrypt_string(str)
111
+ rescue
112
+ $log.debug("error when blowfish encoding")
113
+ end
114
+ return ""
115
+ end
116
+
117
+ def blowfish_decode(str)
118
+ begin
119
+ blowfish = Crypt::Blowfish.new(GNMSPPH)
120
+ return blowfish.decrypt_string(str)
121
+ rescue
122
+ $log.debug("error when blowfish decoding")
123
+ end
124
+ return ""
125
+ end
126
+
127
+ #
128
+ # return current snmp version from version str
129
+ #
130
+ def get_snmp_version(str)
131
+ if str == SNMPv2
132
+ return :SNMPv2c
133
+ else
134
+ return :SNMPv1
135
+ end
136
+ end
137
+
138
+ def get_color_from_status(status)
139
+ case status
140
+ when "CRITICAL"
141
+ return $red
142
+ when "MAJOR"
143
+ return $orange
144
+ when "WARNING"
145
+ return $yellow
146
+ when "MINOR"
147
+ return $blue
148
+ when "NORMAL"
149
+ return $green
150
+ when "UNMANAGED"
151
+ return $grey
152
+ when "UNKNOWN"
153
+ return $purple
154
+ end
155
+ end
156
+
157
+ #
158
+ # convert status to level
159
+ # can return a level or '' if not convert or "UNKNOWN", "UNMANAGED" if "UNMANAGED"
160
+ def get_level_from_status(status)
161
+ case status
162
+ when "UNMANAGED"
163
+ return "UNMANAGED"
164
+ when "CRITICAL"
165
+ return "PANIC"
166
+ when "MAJOR"
167
+ return "ALERT"
168
+ when "WARNING"
169
+ return "ERR"
170
+ when "MINOR"
171
+ return "WARN"
172
+ when "NORMAL"
173
+ return "INFO"
174
+ else
175
+ return ""
176
+ end
177
+ end
178
+
179
+ def get_status_from_level(level)
180
+ case level
181
+ when "UNMANAGED"
182
+ return "UNMANAGED"
183
+ when "PANIC"
184
+ return "CRITICAL"
185
+ when "ALERT"
186
+ return "MAJOR"
187
+ when "ERR"
188
+ return "WARNING"
189
+ when "WARN"
190
+ return "MINOR"
191
+ when "INFO"
192
+ return "NORMAL"
193
+ else
194
+ return "UNKNOWN"
195
+ end
196
+ end
197
+
198
+
199
+ def exist_monitoring_thread(str)
200
+ return $thread_list[str] != nil
201
+ end
202
+
203
+ def add_monitoring_thread(str, thr)
204
+ $log.debug("Starting thread #{str}")
205
+ if thr
206
+ $thread_list[str]=thr
207
+ if thr.class == Thread
208
+ $threads.add(thr)
209
+ else
210
+ $log.error("Thread #{str} could not be loaded")
211
+ end
212
+ else
213
+ $log.debug("Thread #{str} not started")
214
+ end
215
+ end
216
+
217
+ #
218
+ # remove thread from thread group
219
+ #
220
+ def del_monitoring_thread(str)
221
+ if $thread_list[str] != nil
222
+ $log.debug("Removing thread #{str}")
223
+ ThreadGroup.new.add($thread_list[str])
224
+ $thread_list.delete(str)
225
+ end
226
+ end
227
+
228
+ #
229
+ # popup to confirm application exit
230
+ #
231
+ def confirm_exit_window()
232
+ $in_confirm_exit_window=true
233
+ if $config.confirm_quit
234
+ dialog = Gtk::MessageDialog.new($win, Gtk::Dialog::DESTROY_WITH_PARENT,
235
+ Gtk::MessageDialog::QUESTION,
236
+ Gtk::MessageDialog::BUTTONS_YES_NO,
237
+ "Do you really want to quit ?")
238
+ dialog.run do |r|
239
+ dialog.destroy
240
+ if r == Gtk::Dialog::RESPONSE_NO then
241
+ $in_confirm_exit_window=false
242
+ return true
243
+ else
244
+ return false
245
+ end
246
+ end
247
+ else
248
+ return false
249
+ end
250
+ end
251
+
252
+ #
253
+ # fill ip str with prepending zero chars
254
+ #
255
+ def fill_ip_str(ip)
256
+ if ip.nil?
257
+ return nil
258
+ end
259
+ table=ip.split(/\./)
260
+ if table.length != 4
261
+ return nil
262
+ end
263
+ ip_tmp=""
264
+ for i in 0..table.size-1
265
+ ip_tmp+="0"*(3-table[i].length)+table[i]
266
+ ip_tmp+="." unless i==table.size-1
267
+ end
268
+ return ip_tmp
269
+ end
270
+
271
+ def get_map_width()
272
+ if $win.window != nil
273
+ return $win.window.geometry[2]
274
+ else
275
+ return $root_width
276
+ end
277
+ end
278
+
279
+ def get_map_height()
280
+ if $win.window != nil
281
+ return $win.window.geometry[3]
282
+ else
283
+ return $root_height
284
+ end
285
+ end
286
+
287
+ def get_default_map_width()
288
+ if $config.default_width == nil || $config.default_width == ""
289
+ return Gdk::screen_width/2
290
+ else
291
+ return $config.default_width.to_i
292
+ end
293
+ end
294
+
295
+ def get_default_map_height()
296
+ if $config.default_height == nil || $config.default_height == ""
297
+ return Gdk::screen_height/2
298
+ else
299
+ return $config.default_height.to_i
300
+ end
301
+ end
302
+
303
+ def del_selected_node()
304
+ mapp=get_map()
305
+ if $map != nil
306
+ nodes=[]
307
+ $network[mapp].get_node().each_value {|node|
308
+ if node.node_view.selected?()
309
+ nodes.push(node)
310
+ end
311
+ }
312
+ verify_delnodewin(nodes) unless nodes.size() == 0
313
+ end
314
+ end
315
+
316
+ #
317
+ # try to display node of map arg as a grid
318
+ # need: an ip addr map
319
+ #
320
+ def layout_grid(map)
321
+ if map != nil
322
+
323
+ root_width=$win.window.geometry[2]
324
+ root_height=$win.window.geometry[3]
325
+ x=0
326
+ y=0
327
+
328
+ $network[map].get_node().each_value {|node|
329
+ node_v = node.node_view
330
+ if node_v
331
+ node_size=node_v.get_node_size()
332
+ node_v.move(x-node.x, y-node.y)
333
+ x+=node_size+root_width/node_size
334
+ if x >= root_width
335
+ x=0
336
+ y+=node_size+node_size/2+root_height/node_size
337
+ end
338
+ if node.nb_links > 0
339
+ node.get_links().each {|lnk|
340
+ lnk.remove_link
341
+ lnk.display()
342
+ }
343
+ end
344
+ end
345
+ }
346
+ end
347
+ end
348
+
349
+ #
350
+ # return the number of host
351
+ #
352
+ def n_host()
353
+ return $host.size
354
+ end
355
+
356
+ #
357
+ # return the number of network
358
+ #
359
+ def n_network()
360
+ return $network.size
361
+ end
362
+
363
+ #
364
+ # return true if host ip addr is known
365
+ # need: ip
366
+ #
367
+ def exist_host(ip)
368
+ return $host[ip] != nil
369
+ end
370
+
371
+ #
372
+ # del host from a network
373
+ #
374
+ def del_host(ip)
375
+ #we first update network list
376
+ $network[$host[ip].map].del_host(ip)
377
+ $network[$host[ip].map].update_status()
378
+ $host.delete ip
379
+ $event_win.update_statusbar()
380
+ end
381
+
382
+ #
383
+ # add host to a network
384
+ #
385
+ def add_host(ip, mask, map=ROOTMAPADDR)
386
+ if $host[ip] == nil
387
+ $host[ip]=Host.new(ip, mask, nil, nil, map)
388
+ #we update network host list
389
+ if $network[map]
390
+ $network[map].add_host($host[ip])
391
+ $host[ip].node_view.show() if map==get_map()
392
+ $network[map].update_status()
393
+ $event_win.update_statusbar()
394
+ return true
395
+ end
396
+ end
397
+ return false
398
+ end
399
+
400
+ #
401
+ # return true if network ip addr is known
402
+ # need: ip
403
+ #
404
+ def exist_network(ip)
405
+ return $network[ip] != nil
406
+ end
407
+
408
+ #
409
+ # delete a network
410
+ # need: ip
411
+ #
412
+ def del_network(ip)
413
+ if ip != ROOTMAPADDR
414
+ $network[ip].get_node().each_key {|id|
415
+ $host[id].delete() unless $host[id] == nil
416
+ }
417
+ $network[ip].network.each_key {|network_ip|
418
+ $network[network_ip].delete()
419
+ }
420
+ #del network from gui elements
421
+ $network[$network[ip].map].del_net(ip) unless $network[$network[ip].map] == nil
422
+ #del network from network list
423
+ $network.delete ip
424
+ end
425
+ end
426
+
427
+ #
428
+ # add a network
429
+ # need: ip, mask, map
430
+ #
431
+ def add_network(ip, mask, map)
432
+ if map != nil && $network[ip] == nil && $host[ip] == nil
433
+ $network[ip]=Network.new(ip, mask, nil, nil, map)
434
+ $network[ip].node_view.show() if map==get_map()
435
+ #add network in network map
436
+ $network[map].add_network($network[ip])
437
+ return true
438
+ end
439
+ $log.error("IP addr #{ip} already in use") if map != nil
440
+ return false
441
+ end
442
+
443
+ #
444
+ # return table of network ip
445
+ #
446
+ def get_network()
447
+ hnet=[]
448
+ $network.each_value {|node|
449
+ #if node.name?
450
+ # hnet.push node.name
451
+ #else
452
+ hnet.push node.ip
453
+ #end
454
+ }
455
+ return hnet
456
+ end
457
+
458
+ #
459
+ # set current network map window title
460
+ # input: ip map addr
461
+ #
462
+ def set_map_window_title(mp)
463
+ txt=mp
464
+ if txt && $network[txt] && $network[txt].name?()
465
+ txt=$network[txt].name
466
+ end
467
+ $win.set_title("Map #{txt}")
468
+ end
469
+
470
+ #
471
+ # return current network map
472
+ #
473
+ def get_map()
474
+ if $map == nil
475
+ return ROOTMAPADDR
476
+ else
477
+ return $map
478
+ end
479
+ end
480
+
481
+ #
482
+ # set current map by is IP addr
483
+ #
484
+ def set_map(mp=nil)
485
+
486
+ $canvas.get_selected_node().each {|n|
487
+ n.node_view.unselect()
488
+ } unless $canvas == nil
489
+ #first set map
490
+ if $map == nil && mp ==nil && $network[ROOTMAPADDR]
491
+ #puts "1"
492
+ $network[ROOTMAPADDR].node_view.show()
493
+ $network[ROOTMAPADDR].map_view.visibility = Goo::CanvasItem::INVISIBLE
494
+ else
495
+ if $map == nil && $network[ROOTMAPADDR]
496
+ #puts "2"
497
+ #only the network root can have a nil map
498
+ $network[ROOTMAPADDR].get_node().each_value {|node|
499
+ node.node_view.hide_text() unless !node.node_view
500
+ }
501
+ $network[ROOTMAPADDR].node_view.hide()
502
+ else
503
+ if $network[ROOTMAPADDR]
504
+ $network[$map].get_node().each_value {|node|
505
+ node.node_view.hide_text() unless !node.node_view
506
+ }
507
+ $network[$map].map_view.visibility = Goo::CanvasItem::INVISIBLE
508
+ end
509
+ end
510
+ if mp == nil && $network[ROOTMAPADDR]
511
+ #puts "4"
512
+ #only the network root can have a nil map
513
+ $network[ROOTMAPADDR].node_view.resize()
514
+ $network[ROOTMAPADDR].reload()
515
+ if $network[ROOTMAPADDR].set_label_visibility()
516
+ $network[ROOTMAPADDR].change_label($config.network_label_view)
517
+ $network[ROOTMAPADDR].node_view.show_text()
518
+ else
519
+ $network[ROOTMAPADDR].node_view.hide_text()
520
+ end
521
+ $network[ROOTMAPADDR].node_view.show()
522
+ if $config.bg_type == "color"
523
+ $canvas.unset_background_image()
524
+ set_background_color($config.rgb)
525
+ else
526
+ $canvas.set_background_image("#{PIXMAP_PATH}"+"/"+$config.image_path)
527
+ end
528
+ $win.hide_menu_entry()
529
+ else
530
+ #for each node of the new map, we refresh size icon, icon type and label type
531
+ tmp_node=$network[mp]
532
+ if tmp_node
533
+ tmp_node.get_node().each_value {|n|
534
+ n.node_view.resize() unless !n.node_view
535
+ #n.reload()
536
+ if n.set_label_visibility()
537
+ if n.class == Network
538
+ n.change_label($config.network_label_view)
539
+ elsif n.class == Host
540
+ n.change_label($config.host_label_view)
541
+ n.refresh_dns_name()
542
+ end
543
+ n.node_view.show_text() unless !n.node_view
544
+ else
545
+ n.node_view.hide_text() unless !n.node_view
546
+ end
547
+ #show the node icon view
548
+ n.node_view.show() unless !n.node_view
549
+ }
550
+
551
+ $network[mp].map_view.visibility = Goo::CanvasItem::VISIBLE
552
+
553
+ #test $map and mp bg_type ?
554
+ #puts $network[mp].map_bg_type
555
+ if ($network[mp].map_bg_type != nil)
556
+ if $network[mp].map_bg_type == "color"
557
+ if ($network[mp].map_bg_data != $config.rgb) || ($config.bg_type == "image")
558
+ $canvas.unset_background_image()
559
+ set_background_color($network[mp].map_bg_data)
560
+ end
561
+ else
562
+ if ($config.bg_type!=$network[mp].map_bg_type) || ( ($config.bg_type==$network[mp].map_bg_type) && ($network[mp].map_bg_data != $config.image_path))
563
+ $canvas.set_background_image("#{PIXMAP_PATH}"+"/"+$network[mp].map_bg_data)
564
+ end
565
+ end
566
+ else
567
+ if $config.bg_type == "color"
568
+ $canvas.unset_background_image()
569
+ set_background_color($config.rgb)
570
+ else
571
+ if (!$canvas.background_image_isset?()) || ($network[mp].map_bg_data != $config.image_path)
572
+ $canvas.set_background_image("#{PIXMAP_PATH}"+"/"+$config.image_path)
573
+ end
574
+ end
575
+ end
576
+ $win.show_menu_entry()
577
+ end
578
+ end
579
+ $map=mp
580
+ set_map_window_title(mp)
581
+ end
582
+ end
583
+
584
+ #
585
+ # set map window background rgb color
586
+ #
587
+ def window_set_rgb(r, g, b)
588
+ style=Gtk::Style.new
589
+ style.set_bg(Gtk::STATE_NORMAL, r, g, b)
590
+ style.set_bg(Gtk::STATE_ACTIVE, r, g, b)
591
+ style.set_bg(Gtk::STATE_PRELIGHT, r, g, b)
592
+ $win.set_style style
593
+ end
594
+
595
+ #
596
+ # set map window background color
597
+ #
598
+ def set_background_color(rgb)
599
+ r,g,b=rgb.to_a()
600
+ r=r.to_f
601
+ g=g.to_f
602
+ b=b.to_f
603
+ window_set_rgb(r, g, b) unless $win == nil
604
+ $canvas.set_rgb(r, g, b) unless $canvas == nil
605
+ end
606
+
607
+ #
608
+ # select all node of the map
609
+ #
610
+ def select_all_node()
611
+ $network[get_map()].get_node().each_value {|n|
612
+ n.node_view.select()
613
+ }
614
+ end
615
+
616
+ #
617
+ # invert node selection of the map
618
+ #
619
+ def invert_selected_node()
620
+ $network[get_map()].get_node().each_value {|n|
621
+ if n.node_view.selected?()
622
+ n.node_view.unselect()
623
+ else
624
+ n.node_view.select()
625
+ end
626
+ }
627
+ end
628
+
629
+ def check_contact_value(hash)
630
+ hash_bck=Hash.new
631
+ hash.each_key {|id|
632
+ hash_bck[id]=hash[id].dup unless !hash[id]
633
+ }
634
+ hash.each_pair{|key, value|
635
+ if key != nil
636
+ value.each {|contact|
637
+ if !Contact::contact_exist?(contact.firstname, contact.lastname)
638
+ #contact does not exist anymore
639
+ hash_bck[key].delete(contact)
640
+ else
641
+ #contact exist so we set it "in-use"
642
+ Contact::add_use(contact.firstname, contact.lastname)
643
+ end
644
+ } unless !value
645
+ end
646
+ }
647
+ return hash_bck
648
+ end
649
+
650
+
651
+ #
652
+ # switch main window visibility
653
+ #
654
+ def switch_main_visibility()
655
+ if $icon
656
+ $icon.set_blinking(false)
657
+ if $win.visible?
658
+ $win.hide
659
+ else
660
+ $win.show
661
+ end
662
+ end
663
+ end
664
+
665
+ #
666
+ # create main network map window
667
+ #
668
+ def map_window()
669
+ nbuttons = n_host()
670
+
671
+ begin
672
+ #it depends on gnome version
673
+ $icon = Gtk::StatusIcon.new
674
+ $icon.set_pixbuf(Gdk::Pixbuf.new("#{PIXMAP_PATH}/logo_icon.png"))
675
+ $icon.signal_connect('activate') { switch_main_visibility }
676
+ rescue
677
+ $icon=nil
678
+ end
679
+
680
+ $win = App.new("Map")
681
+ $win.set_default_size(get_default_map_width(), get_default_map_height())
682
+ $win.resize(get_default_map_width(), get_default_map_height())
683
+
684
+ #here we set the background color
685
+ window_set_rgb($config.rgb[0].to_f, $config.rgb[1].to_f, $config.rgb[2].to_f)
686
+ $win.signal_connect("delete_event") {
687
+ $root_width=$win.window.geometry[2]
688
+ $root_height=$win.window.geometry[3]
689
+
690
+ #check if the current windowmanager display the trayicon
691
+ if $icon && $icon.embedded?
692
+ #if it's the case we just hide the main window
693
+ $win.hide
694
+ else
695
+ confirm_exit_window()
696
+ end
697
+ }
698
+
699
+ $win.signal_connect("destroy") {
700
+ }
701
+
702
+ $win.signal_connect("configure_event") {|w,e|
703
+ #size or position changed
704
+ if $win != nil
705
+ if (($root_width != e.width) or ($root_height != e.height))
706
+ if (!$win.get_size_changed_from_menu())
707
+ $win.set_display_view_menu()
708
+ end
709
+ $win.set_size_changed_from_menu(false)
710
+ end
711
+ end
712
+ }
713
+
714
+ #read command line options
715
+ file_to_load=nil
716
+ debuglevel=nil
717
+ $testunit=false
718
+ testunitfile=nil
719
+
720
+ opts = OptionParser.new
721
+ opts.on("-f=ARG", "--file filename",String,"Load xml node filename") {|val| file_to_load=val }
722
+ opts.on("-d=ARG", "--debug level",Integer,"Set debug level to default output") {|val| debuglevel=val }
723
+ opts.on("-t=ARG", "--test scenario",String,"Load and execute scenario from test/ dir") {|val| testunitfile=val
724
+ $testunit=true
725
+ }
726
+ opts.on("-h", "--help","Show this help") {|val| puts opts.to_s; exit}
727
+ opts.on("-v", "--version","Display GNMS version") {|val| puts "GNMS version is #{GNMSVERSION}"; exit}
728
+
729
+ begin
730
+ opts.parse(ARGV)
731
+ rescue OptionParser::MissingArgument => msg
732
+ rescue OptionParser::InvalidOption => msg
733
+ rescue OptionParser::InvalidArgument => msg
734
+ $log.error("#{msg}")
735
+ exit
736
+ end
737
+
738
+ if (debuglevel != nil) && (debuglevel >= 0) && (debuglevel < 5)
739
+ $log.level=debuglevel
740
+ end
741
+
742
+ if testunitfile == nil
743
+ if file_to_load != nil
744
+ if FileTest.exist?(file_to_load)
745
+ $log.info("Loading nodes from file #{file_to_load}")
746
+ read_xml(file_to_load)
747
+ else
748
+ $log.error("#{file_to_load} is not an host db file")
749
+ exit
750
+ end
751
+ else
752
+ if FileTest.exist?(SQLITE_DB)
753
+ $log.info("Loading nodes from database")
754
+ db_read()
755
+ #we have to verify if account from conf file and from db matched
756
+ $config.mail_by_level=check_contact_value($config.mail_by_level)
757
+ $config.sms_num_by_level=check_contact_value($config.sms_num_by_level)
758
+ $config.im_dest_by_level=check_contact_value($config.im_dest_by_level)
759
+ else
760
+ create_db()
761
+ #as we don't have account in db we will assure no account come from conf file
762
+ $config.init_hash_conf_by_level($config.mail_by_level)
763
+ $config.init_hash_conf_by_level($config.sms_num_by_level)
764
+ $config.init_hash_conf_by_level($config.im_dest_by_level)
765
+
766
+ if n_host() == 0
767
+ $log.info("Creating default node based on local interfaces")
768
+ create_root_network()
769
+ for interface in local_interface()
770
+ #syntax array of [ipv4 mask ipv6 mac_addr interface_name]
771
+ (ipv4, mask, ipv6, mac_addr, interface_name) = interface
772
+ if !exist_host(ipv4)
773
+ if add_host(ipv4, IPCalc.netmask_to_bits(mask))
774
+ if mac_addr
775
+ $host[ipv4].mac = mac_addr
776
+ end
777
+ if interface_name
778
+ $host[ipv4].add_interface(interface_name)
779
+ end
780
+ #then try to add naming info and os
781
+ tmp = Facter::hostname
782
+ if tmp
783
+ #special case
784
+ if ipv4 == "127.0.0.1"
785
+ tmp="localhost"
786
+ end
787
+ $host[ipv4].name = tmp
788
+ end
789
+ tmp = nil
790
+ tmp = Facter::domain
791
+ if tmp
792
+ $host[ipv4].dns_domain = tmp
793
+ end
794
+ tmp = nil
795
+ tmp = Facter::fqdn
796
+ if tmp
797
+ $host[ipv4].dns_name = tmp
798
+ end
799
+ tmp = nil
800
+ tmp = Facter::kernel
801
+ if tmp
802
+ $host[ipv4].os = tmp.downcase
803
+ end
804
+ tmp = nil
805
+ end
806
+ end
807
+ end
808
+ end
809
+ end
810
+ end
811
+ #set current map
812
+ map_tmp=nil
813
+ if $config.default_map != ""
814
+ if exist_network($config.default_map)
815
+ map_tmp=$config.default_map
816
+ end
817
+ end
818
+ #here we also set the pixmap background
819
+ set_map(map_tmp)
820
+ else
821
+ #execute the test unit script
822
+ $log.info("Running test scenario #{TEST_DIR}/#{testunitfile}")
823
+ File.delete(SQLITE_DB_TEST) unless !FileTest.exist?(SQLITE_DB_TEST)
824
+ File.delete(GLOBAL_CONF_TEST_FILE) unless !FileTest.exist?(GLOBAL_CONF_TEST_FILE)
825
+ create_db(SQLITE_DB_TEST)
826
+ #as we don't have account in db we will assure no account come from conf file
827
+ $config.init_hash_conf_by_level($config.mail_by_level)
828
+ $config.init_hash_conf_by_level($config.sms_num_by_level)
829
+ $config.init_hash_conf_by_level($config.im_dest_by_level)
830
+ create_root_network()
831
+
832
+ begin
833
+ require "#{TEST_DIR}/#{testunitfile}"
834
+ cmd="#{testunitfile[0,1].capitalize()}#{testunitfile[1,testunitfile.size - 1]}"
835
+ (eval cmd).new
836
+ rescue Exception => msg
837
+ print #{msg.backtrace.join("\n")},"\n"
838
+ $log.fatal("Test scenario #{TEST_DIR}/#{testunitfile}.rb does not exist")
839
+ exit()
840
+ end
841
+ end
842
+
843
+ #hide splash_screen
844
+ #splash_screen.hide
845
+ #splash_screen.destroy
846
+
847
+ $win.set_icon_name("MAP")
848
+ $win.set_icon("#{PIXMAP_PATH}/logo_icon.png")
849
+ $win.show
850
+ $root_width, $root_height=$win.size
851
+
852
+ if ! FileTest.exist?(GLOBAL_CONF_FILE)
853
+ $config.show()
854
+ end
855
+
856
+ end
857
+
858
+ def check_scanner_version()
859
+ if $config.nmap_path
860
+ $config.nmap_vers=nmap_version()
861
+ if $config.nmap_vers == nil
862
+ $log.warn("You should install NMAP version from http://www.insecure.org/nmap/ to access full features")
863
+ else
864
+ if $config.nmap_vers.to_i() < 4
865
+ $log.info("You should update NMAP to latest version from http://www.insecure.org/nmap/")
866
+ end
867
+ end
868
+ end
869
+ end
870
+
871
+ #
872
+ # main
873
+ #
874
+ def launch_gnms_main()
875
+
876
+ # begin
877
+
878
+ Thread.abort_on_exception = true
879
+
880
+ $config=ConfigGlobal.new()
881
+ #read config
882
+ $config.read_config_yaml()
883
+
884
+ #set saved log level
885
+ $log.level = txtlvl_to_loggerlvl($config.log_level)
886
+
887
+ #verify nmap version
888
+ check_scanner_version()
889
+
890
+ #create host hash
891
+ $host=Hash.new
892
+
893
+ #create network hash
894
+ $network=Hash.new
895
+
896
+ #create link hash for global operation
897
+ $links=[]
898
+
899
+ #oid ref for snmp monitoring
900
+ $oid_refs=Hash.new
901
+ #wmi ref for wmi monitoring
902
+ $wmi_refs=Hash.new
903
+ #syslog ref for syslog monitoring
904
+ $syslog_refs=Hash.new
905
+ #snmptrap ref for snmptrap monitoring
906
+ $snmptrap_refs=Hash.new
907
+ #http ref for http monitoring
908
+ $http_refs=Hash.new
909
+ #jmx ref for jmx monitoring
910
+ $jmx_refs=Hash.new
911
+
912
+ #create contacts
913
+ $contact=Hash.new
914
+
915
+ Syslog.open(PROJECTNAME, Syslog::LOG_PID | Syslog::LOG_NDELAY)
916
+
917
+ #load some hidden window in background
918
+ $treenode_win=TreeNodeWindow.new
919
+ $event_win=EventWindow.new
920
+
921
+ #by default first map is root map
922
+ $map=nil
923
+ map_window()
924
+
925
+ #we have loaded all node
926
+ $event_win.update_statusbar()
927
+ $treenode_win.reload()
928
+ thread_starting()
929
+
930
+ interfaces_table = local_interface()
931
+ if interfaces_table.size > 0
932
+ ip_of_first_interface = interfaces_table[0][0]
933
+ $event_win.add_event(EventWindow::CORE_EVENT_TYPE,"INFO",$host[ip_of_first_interface], "GNMS launch")
934
+ else
935
+ $event_win.add_event(EventWindow::CORE_EVENT_TYPE,"INFO",$host["127.0.0.1"], "GNMS launch")
936
+ end
937
+
938
+ #rescue Exception => msg
939
+ # puts "-"*80
940
+ # $log.fatal("Please report this error: #{msg}\n#{msg.backtrace.join("\n")}")
941
+ # puts "-"*80
942
+ # end
943
+ end
944
+
945
+ def thread_starting()
946
+ $threads=ThreadGroup.new()
947
+ $thread_list=Hash.new
948
+
949
+ if $config.port_mon
950
+ #thread to listen to monitored port & heartbeat host
951
+ add_monitoring_thread("Port monitoring", tmonitorport())
952
+ end
953
+ if $config.snmp_mon
954
+ #thread to listen to monitored snmp oid refs
955
+ add_monitoring_thread("Snmp monitoring", tmonitorsnmp())
956
+ end
957
+ if $config.wmi_mon
958
+ #thread to listen to monitored wmi refs
959
+ add_monitoring_thread("Wmi monitoring", tmonitorwmi())
960
+ end
961
+ if $config.custom_mon
962
+ #thread to listen to monitored custom cmd
963
+ add_monitoring_thread("Custom monitoring", tmonitorcustom())
964
+ end
965
+ if $config.jmx_mon
966
+ #thread to listen to monitored jmx cmd
967
+ add_monitoring_thread("JMX monitoring", tmonitorjmx())
968
+ end
969
+ if $config.broadcast_ping_state
970
+ #thread to find new host in local network with broacast ping
971
+ add_monitoring_thread("Local ping", t_find_local_segment_pinging())
972
+ #thread to find new host in local segment sniffing for arp/lldp/dhcp packets
973
+ add_monitoring_thread("Local sniffing", t_find_local_segment_sniffing())
974
+ #thread to find new host in remote network with nmap ping
975
+ add_monitoring_thread("Remote ping", t_find_remote_new_host())
976
+ end
977
+ if $config.mac_state
978
+ #thread to find MAC address
979
+ add_monitoring_thread("Find mac", tmacip())
980
+ end
981
+ if $config.node_resolving
982
+ #thread to resolv ip into name
983
+ add_monitoring_thread("Resolve ip to name", tresolvip())
984
+ #thread to resolve ip into netbios name
985
+ add_monitoring_thread("Resolve ip to netbios", tresolvnetbiosname())
986
+ #thread to find os with fingerprinting
987
+ add_monitoring_thread("Find os", tresolvos())
988
+ end
989
+
990
+ if $config.auto_save_map
991
+ add_monitoring_thread("Map autosaving", tauto_save_map())
992
+ end
993
+ $syslog_capture = SyslogCapture.new()
994
+ $snmptrap_capture = SnmpTrapCapture.new()
995
+ $im_daemon = XmppBot.new()
996
+ $irc_daemon = IrcBot.new()
997
+ end
998
+
999
+ def thread_stopping()
1000
+ $threads.list.each {|thr|
1001
+ Thread.kill(thr)
1002
+ Gtk.thread_flush
1003
+ }
1004
+ #kill syslog daemon if needed
1005
+ $syslog_capture.stop() unless $syslog_capture == nil
1006
+ $snmptrap_capture.stop() unless $snmptrap_capture == nil
1007
+ $im_daemon.stop() unless $im_daemon == nil
1008
+ $irc_daemon.stop() unless $irc_daemon == nil
1009
+ end
1010
+
1011
+ def busy?()
1012
+ return $win.busy?()
1013
+ end
1014
+
1015
+ def set_busy()
1016
+ $win.busy()
1017
+ end
1018
+
1019
+ def set_unbusy()
1020
+ $win.unbusy()
1021
+ end
1022
+
1023
+ def quit_now()
1024
+ if !confirm_exit_window()
1025
+ quit_all()
1026
+ end
1027
+ end
1028
+
1029
+ #
1030
+ # call all db method to save all the map in the db
1031
+ #
1032
+ def save_all_in_db()
1033
+ #save nodes
1034
+ set_busy()
1035
+ Thread.start {
1036
+ $host.each_key {|ip|
1037
+ $log.debug("Saving node host #{ip}")
1038
+ nodeid=db_get_node_id(ip, HOSTNODE)
1039
+ db_set_mac(nodeid, $host[ip].mac)
1040
+ db_set_node_type_and_os(nodeid, $host[ip].type, $host[ip].os)
1041
+ db_set_node_position(nodeid, $host[ip].x, $host[ip].y)
1042
+ db_set_severity(nodeid, $host[ip].status, $host[ip].avail_sev, $host[ip].snmp_sev, $host[ip].custom_sev,$host[ip].trap_sev,$host[ip].service_sev,$host[ip].wmi_sev,$host[ip].syslog_sev,$host[ip].jmx_sev)
1043
+ db_set_map(nodeid, $host[ip].map)
1044
+ db_set_name(nodeid, $host[ip].name, $host[ip].dns_name, $host[ip].dns_domain, $host[ip].netbios_name, $host[ip].netbios_domain)
1045
+ db_set_interfaces(ip, $host[ip].interface)
1046
+ }
1047
+
1048
+ $network.each_key {|ip|
1049
+ $log.debug("Saving node network #{ip}")
1050
+ nodeid=db_get_node_id(ip,NETWORKNODE)
1051
+ db_set_node_type_and_os(nodeid, $network[ip].type, $network[ip].os)
1052
+ db_set_node_position(nodeid, $network[ip].x, $network[ip].y)
1053
+ db_set_severity(nodeid, $network[ip].status, $network[ip].avail_sev, $network[ip].snmp_sev, $network[ip].custom_sev, $network[ip].trap_sev,$network[ip].service_sev,$network[ip].wmi_sev,$network[ip].syslog_sev,$network[ip].jmx_sev)
1054
+ db_set_map(nodeid, $network[ip].map)
1055
+ db_set_name(nodeid, $network[ip].name, Node::NONAME_PROPERTY, $network[ip].dns_domain, Node::NONAME_PROPERTY, $network[ip].netbios_domain)
1056
+ db_set_node_size(nodeid, $network[ip].node_size)
1057
+ if ip == ROOTMAPADDR
1058
+ #make sure latest snmp read/write community have been stored
1059
+ db_set_account(nodeid, "snmp", $config.read_community, $config.write_community)
1060
+ end
1061
+ }
1062
+
1063
+ #save links
1064
+ db_del_links()
1065
+ db_set_links()
1066
+ #unset cursor
1067
+ set_unbusy()
1068
+ }
1069
+ end
1070
+
1071
+ def quit_all
1072
+ $config.write_config_yaml()
1073
+ #kill threads
1074
+ thread_stopping()
1075
+ #stop event thread
1076
+ $event_win.stop_analyzer()
1077
+ #we need to save the nodes in db
1078
+ save_all_in_db()
1079
+ sleep(1) until !$win.busy?
1080
+
1081
+ if File.exists?(PID_FILE)
1082
+ begin
1083
+ File.delete(PID_FILE)
1084
+ rescue Exception => msg
1085
+ $log.error(msg)
1086
+ end
1087
+ end
1088
+ Syslog.close if Syslog
1089
+ $win.destroy
1090
+ Gtk.main_quit
1091
+ end