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,215 @@
1
+ class Host < Node
2
+ attr_accessor :mac, :name, :dns_name, :dns_domain, :netbios_name, :netbios_domain, :node_view
3
+
4
+ def initialize (ip="", mask=24, ipv6="", mask6=64, mp=nil, mac="(noarp)",n="(noname)", nn="(noname)",p=nil, s=UNKNOWN, xd=0 ,yd=0, i="", d=NODESCR_PROPERTY, t="host", o="unknown", uuid=create_uuid())
5
+
6
+ super(ip, mask, ipv6, mask6, mp, p, s, xd, yd, i, d, t, o, uuid)
7
+ @mac=mac
8
+ @dns_name=n
9
+ @netbios_name=nn
10
+ @name=NONAME_PROPERTY
11
+ @dns_domain=NONAME_PROPERTY
12
+ @netbios_domain=NONAME_PROPERTY
13
+ @node_view=nil
14
+
15
+ if mp != nil
16
+ if $network[mp].status == UNMANAGED
17
+ self.status=UNMANAGED
18
+ end
19
+ end
20
+
21
+ #create gui view
22
+ create_node()
23
+ #store node in db
24
+ caller_func = caller[1][/`.*'/][1..-2]
25
+ write_db() unless caller_func == "db_read_node"
26
+ end
27
+
28
+ def mac?()
29
+ return mac != NOARP_PROPERTY
30
+ end
31
+
32
+ def hostname?()
33
+ return name != NONAME_PROPERTY
34
+ end
35
+
36
+ def dns_name?()
37
+ return dns_name != NONAME_PROPERTY
38
+ end
39
+
40
+ def netbios_name?()
41
+ return netbios_name != NONAME_PROPERTY
42
+ end
43
+
44
+ def get_dns_name()
45
+ if dns_name?() or @name == ""
46
+ if $config.noshow_fqdn
47
+ dns_name_tab="#{@dns_name}".split(/\./)
48
+ return dns_name_tab[0]
49
+ else
50
+ return @dns_name
51
+ end
52
+ else
53
+ #if no fqdn set return the hostname
54
+ return @name
55
+ end
56
+ end
57
+
58
+ def refresh_dns_name()
59
+ if $config.host_label_view == "dns_name"
60
+ @node_view.set_text(get_dns_name())
61
+ end
62
+ end
63
+
64
+ #
65
+ # method for changing status (color) of the node
66
+ # st is a $status_value
67
+ #
68
+ def change_status(st)
69
+ if @status != st
70
+ #status has changed
71
+ @status=st
72
+ @node_view.icon_color($status_color[$status_value.index(st)]) unless @node_view==nil
73
+ @links.each {|l|
74
+ l.update_status()
75
+ }
76
+
77
+ if $treenode_win.visible?
78
+ $treenode_win.update_row(self)
79
+ end
80
+ #update status bar stats if not locked
81
+ if !$event_win.updating_statusbar?()
82
+ $event_win.update_statusbar()
83
+ end
84
+ #update status of the network unless st is UNMANAGED or UNKNOWN
85
+ if (st != UNMANAGED) && (st != UNKNOWN)
86
+ $network[@map].update_status()
87
+ end
88
+ end
89
+ end
90
+
91
+ def change_type(typ)
92
+ if @type != typ
93
+ @node_view.set_type(typ)
94
+ end
95
+ @type=typ
96
+ end
97
+
98
+ #
99
+ # method for changing label of the node
100
+ # lb is nil when we just want a node label refresh
101
+ #
102
+ def change_label(lb=nil)
103
+ if $config.show_label
104
+ if (@old_label != lb) or lb==nil
105
+ @old_label=lb unless lb==nil
106
+ lb=@old_label unless lb!=nil
107
+ if lb == "mac_manufacturer" && mac?
108
+ if (@mac[0,1] >= '0' && @mac[0,1] <= '9')
109
+ macplus=@mac.split(/:/)
110
+ #0,1,2 are reserved for the manufacturer
111
+ macbeg=""
112
+ for i in 0..2
113
+ if macplus[i].size == 1 #one digit
114
+ macbeg="#{macbeg}0#{macplus[i]}"
115
+ else
116
+ macbeg="#{macbeg}#{macplus[i]}"
117
+ end
118
+ end
119
+ manufacturer=find_manufacturer(macbeg)
120
+ if manufacturer != nil
121
+ macplus="#{manufacturer}:#{macplus[3]}:#{macplus[4]}:#{macplus[5]}"
122
+ node_view.set_text("#{macplus.to_s}")
123
+ end
124
+ else
125
+ node_view.set_text(get_dns_name())
126
+ end
127
+ else
128
+ if ((lb == "mac")||(lb == "mac_manufacturer")) && !mac?
129
+ node_view.set_text("#{@ip}")
130
+ else
131
+ if lb == "dns_name"
132
+ node_view.set_text(get_dns_name())
133
+ else
134
+ node_view.set_text("#{eval lb}")
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ def create_node()
143
+ @old_label=""
144
+ #set default label
145
+ label=get_dns_name()
146
+ if !dns_name? && ip != ""
147
+ label="#{ip}"
148
+ end
149
+
150
+ if @map != nil
151
+ #print "host ", $network[@map].ip, "\n"
152
+ @node_view=NodeHostView.new($network[@map].map_view, self, $status_color[$status_value.index(@status)], "#{PIXMAP_PATH}/#{@icon}", label)
153
+ #@node_view.hide()
154
+ change_label($config.host_label_view)
155
+ if !$config.show_label
156
+ @node_view.hide_text()
157
+ end
158
+ end
159
+ end #create_node
160
+
161
+ def delete()
162
+ #destroy the links
163
+ del_all_links()
164
+ #destroy the tooltips
165
+ @node_view.hide_tooltips()
166
+ #destroy the view
167
+ @node_view.destroy()
168
+ if $treenode_win.visible?
169
+ $treenode_win.del_row(self)
170
+ end
171
+ #remove from db
172
+ db_delete_node(HOSTNODE, ip, netmask)
173
+
174
+ del_host(@ip)
175
+ end
176
+
177
+ #
178
+ # 1 is avail 2 is snmp 3 is custom 4 is trap
179
+ #
180
+ def set_severities(avail, snmp, custom, trap, serv, wmi, syslog, jmx)
181
+ @avail_sev=avail
182
+ @snmp_sev=snmp
183
+ @custom_sev=custom
184
+ @trap_sev=trap
185
+ @service_sev=serv
186
+ @wmi_sev=wmi
187
+ @syslog_sev=syslog
188
+ @jmx_sev=jmx
189
+ refresh_status()
190
+ end
191
+
192
+ #reload pixmap icon to apply changes
193
+ def reload()
194
+ if ($config.host_icon_view=="os")
195
+ @icon="#{find_extension("os/#{@os}")}"
196
+ else
197
+ @icon="#{find_extension("type/#{@type}")}"
198
+ end
199
+ @node_view.set_image(@icon)
200
+ end
201
+
202
+ #
203
+ # Return a table for the find tool
204
+ #
205
+ def to_t
206
+ tmp=[]
207
+ tmp.push(@ip,$status[$status_value.index(@status)], @mac, @dns_name, @netbios_name, @description, @type, @os)
208
+ return tmp
209
+ end
210
+
211
+ def write_db()
212
+ return db_insert_node_prp(uuid,HOSTNODE,ip,netmask,ipv6,netmask6,mac,map,status,avail_sev,snmp_sev,custom_sev,trap_sev, service_sev, wmi_sev, syslog_sev, jmx_sev, x,y,icon,description, nil, nil, nil,nil,type,os,"",dns_name,"",netbios_name,"",nil,nil,nil)
213
+ end
214
+
215
+ end #class
@@ -0,0 +1,639 @@
1
+ #
2
+ # Thread to listen for hosts
3
+ #
4
+ require 'resolv'
5
+
6
+ #
7
+ # auto save map
8
+ #
9
+ def tauto_save_map()
10
+ $log.debug("in tauto_save_map")
11
+ Thread.start {
12
+ while $config.auto_save_map
13
+ for minute in 1..$config.auto_save_map_value.to_i
14
+ sleep(60)
15
+ if !$config.auto_save_map
16
+ break
17
+ end
18
+ end
19
+ $log.info("Auto Saving map every #{$config.auto_save_map_value} minutes launch")
20
+ write_xml()
21
+ end
22
+ del_monitoring_thread("Map autosaving")
23
+ }
24
+ end
25
+
26
+ #
27
+ # monitor custom
28
+ #
29
+ def tmonitorcustom()
30
+ Thread.start {
31
+ while $config.custom_mon
32
+ $log.debug("in tmonitorcustom every #{$config.custom_mon_delay} second")
33
+ sleep($config.custom_mon_delay.to_i)
34
+ ip_table=$host.keys
35
+ for ip in ip_table
36
+ if !$config.custom_mon
37
+ break
38
+ end
39
+ begin
40
+ if $host[ip].status != UNMANAGED
41
+ test_monitored_custom(ip)
42
+ end
43
+ rescue Exception => msg
44
+ $log.error("Custom monitor: #{msg}")
45
+ $event_win.add_event(EventWindow::CUSTOM_EVENT_TYPE, "ERR", $host[ip], msg)
46
+ end
47
+ end
48
+ end
49
+ }
50
+ end
51
+
52
+ #
53
+ # monitor snmp oid refs
54
+ #
55
+ def tmonitorsnmp()
56
+ Thread.start {
57
+ while $config.snmp_mon
58
+ $log.debug("in tmonitorsnmp every #{$config.snmp_mon_delay} second")
59
+ sleep($config.snmp_mon_delay.to_i)
60
+ ip_table=$host.keys
61
+ for ip in ip_table
62
+ if !$config.snmp_mon
63
+ break
64
+ end
65
+ begin
66
+ if $host[ip] && ($host[ip].status != UNMANAGED)
67
+ test_monitored_snmp(ip)
68
+ end
69
+ rescue Exception => msg
70
+ $log.error(msg)
71
+ $event_win.add_event(EventWindow::SNMP_EVENT_TYPE, "INFO", $host[ip], msg)
72
+ end
73
+ end
74
+ end
75
+ }
76
+ end
77
+
78
+ #
79
+ # monitor wmi refs
80
+ #
81
+ def tmonitorwmi()
82
+ Thread.start {
83
+ while $config.wmi_mon
84
+ $log.debug("in tmonitorwmi every #{$config.wmi_mon_delay} second")
85
+ sleep($config.wmi_mon_delay.to_i)
86
+ ip_table=$host.keys
87
+ for ip in ip_table
88
+ if !$config.wmi_mon
89
+ break
90
+ end
91
+ begin
92
+ if $host[ip] && ($host[ip].status != UNMANAGED)
93
+ test_monitored_wmi(ip)
94
+ end
95
+ rescue Exception => msg
96
+ $log.error(msg)
97
+ $event_win.add_event(EventWindow::WMI_EVENT_TYPE, "INFO", $host[ip], msg)
98
+ end
99
+ end
100
+ end
101
+ }
102
+ end
103
+
104
+ #
105
+ # monitor jmx refs
106
+ #
107
+ def tmonitorjmx()
108
+ Thread.start {
109
+ while $config.jmx_mon
110
+ $log.error("in tmonitorjmx every #{$config.jmx_mon_delay} second")
111
+ sleep($config.jmx_mon_delay.to_i)
112
+ ip_table=$host.keys
113
+ for ip in ip_table
114
+ if !$config.jmx_mon
115
+ break
116
+ end
117
+ begin
118
+ if $host[ip] && ($host[ip].status != UNMANAGED)
119
+ test_monitored_jmx(ip)
120
+ end
121
+ rescue Exception => msg
122
+ $log.error(msg)
123
+ $event_win.add_event(EventWindow::JMX_EVENT_TYPE, "INFO", $host[ip], msg)
124
+ end
125
+ end
126
+ end
127
+ }
128
+ end
129
+
130
+ #
131
+ # monitor host and service(s)
132
+ #
133
+ def tmonitorport()
134
+ Thread.start {
135
+ while $config.port_mon
136
+ $log.debug("in tmonitorport every #{$config.port_mon_delay} second")
137
+ sleep($config.port_mon_delay.to_i)
138
+ #retry already known hosts, if NORMAL test his monitored ports
139
+ ip_table=$host.keys
140
+ for ip in ip_table
141
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
142
+ if $host[ip] != nil && $host[ip].status != UNMANAGED
143
+ #puts "trying .... #{ip}"
144
+ if ping(ip)
145
+ #test monitored ports
146
+ #puts "ping ok here #{ip} #{$host[ip].service.size()}"
147
+ if $config.port_mon && $host[ip] && ($host[ip].service.size() > 0)
148
+ #test if monitored ports are ok
149
+ sev_st = test_monitored_ports(ip)
150
+ if sev_st <= 0
151
+ $host[ip].set_service_severity(NORMAL)
152
+ else
153
+ $host[ip].set_service_severity(sev_st)
154
+ end
155
+ end
156
+ #host is up
157
+ if $host[ip] && $host[ip].avail_sev != NORMAL
158
+ $host[ip].set_avail_severity(NORMAL)
159
+ $event_win.add_event(EventWindow::AVAIL_EVENT_TYPE, "INFO", $host[ip], "Ping OK")
160
+ end
161
+ else
162
+ #puts "ping nok here #{ip} #{$host[ip].service.size()}"
163
+ #test monitored ports here too, as the ping can be disabled but services up
164
+ if $config.port_mon && $host[ip] && ($host[ip].service.size() > 0)
165
+ #test if monitored ports are ok
166
+ sev_st = test_monitored_ports(ip)
167
+ if sev_st <= 0
168
+ $host[ip].set_service_severity(NORMAL)
169
+ else
170
+ $host[ip].set_service_severity(sev_st)
171
+ end
172
+ end
173
+ #host is down
174
+ if $host[ip] != nil
175
+ #puts "ping nok #{ip}"
176
+ if $host[ip].avail_sev != CRITICAL
177
+ $host[ip].set_avail_severity(CRITICAL)
178
+ end
179
+ #here we only push the event when only the availability sev is set
180
+ if (($host[ip].avail_sev == CRITICAL) and ($host[ip].snmp_sev <= UNMANAGED) and ($host[ip].custom_sev <= UNMANAGED) and ($host[ip].trap_sev <= UNMANAGED) and ($host[ip].service_sev <= UNMANAGED) and ($host[ip].wmi_sev <= UNMANAGED) and ($host[ip].syslog_sev <= UNMANAGED) and ($host[ip].jmx_sev <= UNMANAGED))
181
+ $event_win.add_event(EventWindow::AVAIL_EVENT_TYPE, "PANIC", $host[ip], "Ping KO")
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
188
+ end
189
+ }
190
+ end
191
+
192
+ #
193
+ # try to find os for given ip
194
+ #
195
+ def tresolvos()
196
+ Thread.start {
197
+ while $config.node_resolving
198
+ $log.debug("in tresolos #{$config.node_resolving_delay.to_i} second")
199
+ sleep($config.node_resolving_delay.to_i) #wait before all window are maps
200
+ ip_table=$host.keys
201
+ for ip in ip_table
202
+ if !$config.node_resolving
203
+ break
204
+ end
205
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
206
+ #puts "for #{ip}"
207
+ if $host[ip] && ($host[ip].os == "unknown") && ($host[ip].status != CRITICAL) && ($host[ip].status != UNMANAGED)
208
+ begin
209
+ Timeout::timeout($config.node_resolving_delay.to_i) do |p|
210
+ Thread.start {
211
+ res=osfingerprint("#{ip}")
212
+ if res != "" && res != nil
213
+ $host[ip].os = res unless !$host[ip]
214
+ $host[ip].change_label() unless !$host[ip]
215
+ $host[ip].reload unless !$host[ip]
216
+ end
217
+ }
218
+ sleep(p)
219
+ end
220
+ rescue Timeout::Error
221
+ $log.debug("tresolvos(): difficulty to find OS for address #{ip}")
222
+ end
223
+ end
224
+ end
225
+ end
226
+ sleep($config.node_resolving_delay.to_i)
227
+ end
228
+ }
229
+ end
230
+
231
+ #
232
+ # try to find a netbios name for an IP
233
+ #
234
+ def tresolvnetbiosname()
235
+ Thread.start {
236
+ while $config.node_resolving
237
+ sleep($config.node_resolving_delay.to_i) #wait before all window are maps
238
+ ip_table=$host.keys
239
+ for ip in ip_table
240
+ if !$config.node_resolving
241
+ break
242
+ end
243
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
244
+ if $host[ip] && $host[ip].netbios_name == "(noname)"
245
+ begin
246
+ timeout($config.node_resolving_delay.to_i) {
247
+ Thread.start {
248
+ res=ip2netbiosname("#{ip}")
249
+ if res != nil
250
+ $host[ip].netbios_name = res
251
+ $host[ip].change_label()
252
+ end
253
+ }
254
+ sleep(5)
255
+ }
256
+ rescue
257
+ $log.error("Difficulty to resolv #{ip} netbios name")
258
+ end
259
+ end
260
+ end
261
+ end
262
+ sleep($config.node_resolving_delay.to_i)
263
+ end
264
+ }
265
+ end
266
+
267
+ #
268
+ # try to find a name for an IP
269
+ #
270
+ def tresolvip()
271
+ Thread.start {
272
+ while $config.node_resolving
273
+ $log.debug("in tresolvip every #{$config.node_resolving_delay.to_i} second")
274
+ sleep($config.node_resolving_delay.to_i) #wait before all window are maps
275
+ ip_table=$host.keys
276
+ for ip in ip_table
277
+ if !$config.node_resolving
278
+ break
279
+ end
280
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
281
+ if $host[ip] && !$host[ip].dns_name?
282
+ begin
283
+ timeout($config.node_resolving_delay.to_i) {
284
+ Thread.start {
285
+ #puts "in resolv ip #{ip}"
286
+ begin
287
+ res=Resolv.getname("#{ip}").to_s
288
+ if res != ""
289
+ #puts "resolved! #{res}"
290
+ $host[ip].dns_name = res
291
+ $host[ip].change_label()
292
+ end
293
+ rescue Resolv::ResolvError
294
+ $log.debug("tresolvip(): cant find dns name for address #{ip}");
295
+ end
296
+ }
297
+ sleep(5)
298
+ }
299
+ rescue
300
+ $log.error("Difficulty to resolv #{ip}, add it to your /etc/hosts file")
301
+ end
302
+ end
303
+ end
304
+ end
305
+ sleep($config.node_resolving_delay.to_i)
306
+ end
307
+ }
308
+ end
309
+
310
+ #
311
+ # snmp discovery
312
+ # read arp, route, interface from a snmp host
313
+
314
+ def tresolvesnmp()
315
+ #atNetAddress
316
+ #ipRouteNextHop
317
+
318
+ Thread.start {
319
+ while $config.node_resolving
320
+ $log.debug("in tresolvesnmp every #{$config.node_resolving_delay.to_i} second")
321
+ sleep($config.node_resolving_delay.to_i) #wait before all window are maps
322
+ ip_table=$host.keys
323
+ for ip in ip_table
324
+ if !$config.node_resolving
325
+ break
326
+ end
327
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
328
+ #code here
329
+
330
+ end
331
+ end
332
+ sleep($config.node_resolving_delay.to_i)
333
+ end
334
+ }
335
+ end
336
+
337
+ #
338
+ # try to find a mac address for an IP
339
+ #
340
+ def tmacip ()
341
+ Thread.start {
342
+ while $config.mac_state
343
+ sleep($config.mac_delay.to_i) #wait before all window are maps
344
+ ip_table=$host.keys
345
+ for ip in ip_table
346
+ if !$config.mac_state
347
+ break
348
+ end
349
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
350
+ if $host[ip] && $host[ip].mac == "(noarp)"
351
+ begin
352
+ timeout($config.mac_delay.to_i) {
353
+ Thread.start {
354
+ res=mac(ip)
355
+ if res != ""
356
+ $host[ip].mac = res
357
+ $host[ip].change_label()
358
+ end
359
+ }
360
+ sleep(5)
361
+ }
362
+ rescue
363
+ $log.info("difficulty to find MAC address for #{ip}")
364
+ end
365
+ else
366
+ if $config.mac_lock #we verify mac address
367
+ begin
368
+ timeout($config.mac_delay.to_i) {
369
+ Thread.start {
370
+ res=mac(ip)
371
+ if res != ""
372
+ if $host[ip] && $host[ip].mac != res && $host[ip].status != CRITICAL
373
+ $host[ip].set_avail_severity(MINOR)
374
+ $event_win.add_event(EventWindow::AVAIL_EVENT_TYPE, "WARN", $host[ip], "Host MAC@ changed")
375
+ else
376
+ if $host[ip].status == MINOR
377
+ $host[ip].set_avail_severity(CRITICAL)
378
+ end
379
+ end
380
+ end
381
+ }
382
+ sleep(5)
383
+ }
384
+ rescue
385
+ end
386
+ end
387
+ end
388
+ end
389
+ end
390
+ sleep($config.mac_delay.to_i)
391
+ end
392
+ }
393
+ end
394
+
395
+ #
396
+ # try to find new host in another network
397
+ #
398
+ def tfindremotehost (map, mask)
399
+ Thread.start {
400
+ lhost=broadcast_remote_ping(map, mask)
401
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
402
+ for ip in lhost
403
+ add_host_entry_with_map(ip, mask, map)
404
+ end
405
+ end
406
+ }
407
+ end
408
+
409
+ def t_find_remote_new_host ()
410
+ Thread.start {
411
+ while $config.broadcast_ping_state
412
+ sleep($config.broadcast_ping_delay.to_i)
413
+ $log.debug("in t_find_remote_new_host")
414
+ $network.each_value {|node|
415
+ if !$config.broadcast_ping_state
416
+ break
417
+ end
418
+ if node.ip != ROOTMAPADDR
419
+ lhost=broadcast_remote_ping(node.ip, node.netmask)
420
+ #we secure the function call after
421
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
422
+ for ip in lhost
423
+ #puts "#{ip} dans t_find_remote_new_host"
424
+ add_host_entry_with_map(ip, node.netmask, node.ip)
425
+ end
426
+ end
427
+ end
428
+ }
429
+ sleep($config.broadcast_ping_delay.to_i)
430
+ end
431
+ }
432
+ end
433
+
434
+ #
435
+ # try to find new host in a given range IP addr
436
+ #
437
+ def tfindrangeremotehost (ip_begin, ip_end)
438
+ Thread.start {
439
+ $log.debug("in tfindrangeremotehost")
440
+ lhost=range_remote_ping(ip_begin, ip_end)
441
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
442
+ for ip in lhost
443
+ #puts "findrangeremotehost #{ip}"
444
+ add_host_entry_with_map(ip, 24, get_map())
445
+ end
446
+ end
447
+ }
448
+ end
449
+
450
+ #
451
+ # listen for new host in local network (broadcast)
452
+ #
453
+ def t_find_local_segment_pinging()
454
+ Thread.start {
455
+ while $config.broadcast_ping_state
456
+ sleep($config.broadcast_ping_delay.to_i)
457
+ if !defined?($in_confirm_exit_window) || ($in_confirm_exit_window == false)
458
+ mask=local_mask()
459
+ mask = "255.255.255.0" if !mask
460
+ for ip in broadcast_ping
461
+ #puts "t_find_local_segment_pinging #{ip} #{mask}"
462
+ #test if network was removed during the time
463
+ if ($network_map_always == 1) and (!$network.has_key?($network_map_result))
464
+ $network_map_always = 0
465
+ $network_map_result = nil
466
+ end
467
+ if $host and $host[ip].nil?
468
+ if $network_map_always == 0
469
+ choose_network_map(ip, IPCalc.get_network_addr(ip, mask))
470
+ while $network_map_result.nil?
471
+ sleep(2)
472
+ end
473
+ end
474
+ if !$network_map_result.nil? and !$network_map_result.empty?
475
+ map = $network_map_result
476
+ if $network.has_key?(map)
477
+ addhostentry(ip, mask, map)
478
+ sleep(2)
479
+ end
480
+ end
481
+ if $network_map_always == 0
482
+ $network_map_result = nil
483
+ end
484
+ end
485
+ end
486
+ end
487
+ sleep($config.broadcast_ping_delay.to_i)
488
+ end
489
+ }
490
+ end
491
+
492
+ #
493
+ # listen for new host in local network,
494
+ # sniffing for packets arp, lldp/cdp as now
495
+ #
496
+ def t_find_local_segment_sniffing()
497
+ #run as root ?
498
+ if Process.euid == 0
499
+ begin
500
+ require 'pcaprub'
501
+ require 'packetfu'
502
+ rescue LoadError => msg
503
+ $log.error("Missing library #{msg}")
504
+ return
505
+ end
506
+
507
+ iface = Pcap.lookupdev
508
+ if iface
509
+ Thread.start {
510
+ cap = PacketFu::Capture.new(:iface => iface, :start => false)
511
+ begin
512
+ cap.bpf(:filter => 'not ip')
513
+ rescue
514
+ $log.info("Can't set the bpf filter for sniff packets")
515
+ end
516
+ cap.start
517
+ loop do
518
+ cap.stream.each do |p|
519
+ pkt = PacketFu::Packet.parse p
520
+ if pkt.is_arp?
521
+ #check if it's ethernet and arp reply
522
+ if pkt.arp_hw == 1 and pkt.arp_opcode == 2
523
+ ip_mac_t = Array.new
524
+ ip_mac_t.push([pkt.arp_saddr_ip, pkt.arp_saddr_mac])
525
+ ip_mac_t.push([pkt.arp_daddr_ip, pkt.arp_daddr_mac])
526
+ ip_mac_t.each {|ip, mac|
527
+ $log.debug("t_find_local_segment_sniffing: ARP reply founds ip=#{ip} mac=#{mac}")
528
+ if isValidIPv4(ip) and !exist_host(ip)
529
+ if $network_map_always == 0
530
+ choose_network_map(ip)
531
+ while $network_map_result.nil?
532
+ sleep(2)
533
+ end
534
+ end
535
+ if !$network_map_result.nil? and !$network_map_result.empty?
536
+ map = $network_map_result
537
+ if $network.has_key?(map)
538
+ addhostentry(ip, 24, map)
539
+ sleep(2)
540
+ if $host[ip]
541
+ $host[ip].mac = mac
542
+ end
543
+ end
544
+ end
545
+ if $network_map_always == 0
546
+ $network_map_result = nil
547
+ end
548
+ end
549
+ }
550
+ end
551
+ end
552
+ begin
553
+ if pkt.is_lldp?
554
+ $log.debug("t_find_local_segment_sniffing: LLDP founds ip=#{pkt.lldp_address} mac=#{pkt.lldp_saddr_mac}")
555
+ if (pkt.lldp_address_type_readable == "IPv4") and isValidIPv4(pkt.lldp_address) and !exist_host(pkt.lldp_address)
556
+ puts pkt.lldp_address
557
+ if $network_map_always == 0
558
+ choose_network_map(pkt.lldp_address)
559
+ while $network_map_result.nil?
560
+ sleep(2)
561
+ end
562
+ end
563
+ if !$network_map_result.nil? and !$network_map_result.empty?
564
+ map = $network_map_result
565
+ if $network.has_key?(map)
566
+ addhostentry(pkt.lldp_address, 24, map)
567
+ sleep(2)
568
+ if $host[pkt.lldp_address]
569
+ $host[pkt.lldp_address].mac = pkt.lldp_saddr_mac
570
+ $host[pkt.lldp_address].name = pkt.lldp_system_name
571
+ end
572
+ end
573
+ end
574
+ if $network_map_always == 0
575
+ $network_map_result = nil
576
+ end
577
+ end
578
+ end
579
+ rescue NoMethodError
580
+ #current gem version does not support lldp patch i sent
581
+ end
582
+ end
583
+ end
584
+ }
585
+ end
586
+ else
587
+ $log.error("t_find_local_segment_sniffing: Not enough privileges to sniff network packets, rerun as root")
588
+ return
589
+ end
590
+ end
591
+
592
+ #
593
+ # To know if we already know this ip
594
+ #
595
+ def contain(iplocal)
596
+ i=0
597
+ $host.each_key{|ip|
598
+ if ip == iplocal
599
+ return i
600
+ end
601
+ i=i+1
602
+ }
603
+ return -1
604
+ end
605
+
606
+ #
607
+ # Add new lign entry > pixmap & host-ip-button
608
+ #
609
+ def add_host_entry_with_map (ip, mask, map)
610
+ #if host doesnt exist in array
611
+ $log.debug("begin add_host_entry_with_map: #{ip}, #{mask}, #{map}")
612
+ #test is done in add_network
613
+ add_network(map, mask, get_map())
614
+
615
+ if $host[ip] == nil
616
+ $event_win.add_event(EventWindow::CORE_EVENT_TYPE, "INFO", $host[ip], "Find new host")
617
+ Gtk.thread_protect do
618
+ add_host(ip,mask,map)
619
+ end
620
+ end
621
+ $log.debug("end add_host_entry_with_map")
622
+ end
623
+
624
+ #
625
+ # Add new lign entry > pixmap & host-ip-button
626
+ #
627
+ def addhostentry(ip, mask, map=nil)
628
+ #if host doesnt exist in array
629
+ if $host[ip] == nil
630
+ $event_win.add_event(EventWindow::CORE_EVENT_TYPE, "INFO", $host[ip], "Find new host")
631
+ Gtk.thread_protect do
632
+ if map
633
+ add_host(ip, mask, map)
634
+ else
635
+ add_host(ip,mask)
636
+ end
637
+ end
638
+ end
639
+ end