1 | 85 | simandl | <?php |
2 | | | |
3 | | | # defaults. Should be overwritten by the cacti config. |
4 | | | $cacti_base = '../../'; |
5 | | | $cacti_url = '/'; |
6 | | | |
7 | | | $width = 4000; |
8 | | | $height = 3000; |
9 | | | |
10 | | | |
11 | | | require_once 'editor-config.php'; |
12 | | | |
13 | | | // check if the goalposts have moved |
14 | | | if( is_dir($cacti_base) && file_exists($cacti_base."/include/global.php") ) |
15 | | | { |
16 | | | // include the cacti-config, so we know about the database |
17 | | | include_once($cacti_base."/include/global.php"); |
18 | | | $config['base_url'] = (isset($config['url_path'])? $config['url_path'] : $cacti_url); |
19 | | | $cacti_found = TRUE; |
20 | | | } |
21 | | | elseif( is_dir($cacti_base) && file_exists($cacti_base."/include/config.php") ) |
22 | | | { |
23 | | | // include the cacti-config, so we know about the database |
24 | | | include_once($cacti_base."/include/config.php"); |
25 | | | $config['base_url'] = (isset($config['url_path'])? $config['url_path'] : $cacti_url); |
26 | | | $cacti_found = TRUE; |
27 | | | } |
28 | | | else |
29 | | | { |
30 | | | print "You need to fix your editor-config.php\n"; |
31 | | | exit(); |
32 | | | } |
33 | | | |
34 | | | include_once($cacti_base."/lib/snmp.php"); |
35 | | | |
36 | | | if(!function_exists("cacti_snmp_get")) |
37 | | | { |
38 | | | die("Cacti SNMP functions are not available"); |
39 | | | } |
40 | | | |
41 | | | # figure out which template has interface traffic. This might be wrong for you. |
42 | | | $data_template = "Interface - Traffic"; |
43 | | | $data_template_id = db_fetch_cell("select id from data_template where name='".mysql_real_escape_string($data_template)."'"); |
44 | | | |
45 | | | $Interfaces_SQL = "select host.snmp_version,host.snmp_community,host.snmp_username,host.snmp_password,host.snmp_auth_protocol,host.snmp_priv_passphrase,host.snmp_priv_protocol,host.snmp_context,host.snmp_port,host.snmp_timeout,host.description, host.hostname, host.disabled, host_snmp_cache.* from host_snmp_cache,host where host_snmp_cache.host_id=host.id and (field_name='ifDescr' or field_name='ifName' or field_name='ifIP' or field_name='ifAlias') and host.disabled<>'on' and field_value<>'127.0.0.1' and field_value<>'0.0.0.0' and host.status=3 and host.snmp_version>0;"; |
46 | | | $queryrows = db_fetch_assoc($Interfaces_SQL); |
47 | | | |
48 | | | if( is_array($queryrows) && sizeof($queryrows) > 0 ) |
49 | | | { |
50 | | | foreach ($queryrows as $line) |
51 | | | { |
52 | | | $key =sprintf( "%06d-%010d",$line['host_id'],$line['snmp_index']); |
53 | | | $hosts[$line['host_id']]['description'] = $line['description']; |
54 | | | $hosts[$line['host_id']]['hostname'] = $line['hostname']; |
55 | | | |
56 | | | $hosts[$line['host_id']]['snmp_version'] = $line['snmp_version']; |
57 | | | $hosts[$line['host_id']]['snmp_username'] = $line['snmp_username']; |
58 | | | $hosts[$line['host_id']]['snmp_password'] = $line['snmp_password']; |
59 | | | $hosts[$line['host_id']]['snmp_auth_protocol'] = $line['snmp_auth_protocol']; |
60 | | | $hosts[$line['host_id']]['snmp_context'] = $line['snmp_context']; |
61 | | | $hosts[$line['host_id']]['snmp_port'] = $line['snmp_port']; |
62 | | | $hosts[$line['host_id']]['snmp_timeout'] = $line['snmp_timeout']; |
63 | | | $hosts[$line['host_id']]['snmp_priv_protocol'] = $line['snmp_priv_protocol']; |
64 | | | $hosts[$line['host_id']]['snmp_priv_passphrase'] = $line['snmp_priv_passphrase']; |
65 | | | $hosts[$line['host_id']]['snmp_community'] = $line['snmp_community']; |
66 | | | |
67 | | | $interfaces[$key]['index'] = $line['snmp_index']; |
68 | | | $interfaces[$key]['host'] = $line['host_id']; |
69 | | | if($line['field_name'] == 'ifIP') $interfaces[$key]['ip'] = $line['field_value']; |
70 | | | if($line['field_name'] == 'ifName') $interfaces[$key]['name'] = $line['field_value']; |
71 | | | if($line['field_name'] == 'ifDescr') $interfaces[$key]['descr'] = $line['field_value']; |
72 | | | if($line['field_name'] == 'ifAlias') $interfaces[$key]['alias'] = $line['field_value']; |
73 | | | } |
74 | | | } |
75 | | | |
76 | | | $count=0; |
77 | | | if(file_exists("mapper-cache.txt")) |
78 | | | { |
79 | | | print "Reading Netmask cache...\n"; |
80 | | | $fd = fopen("mapper-cache.txt","r"); |
81 | | | while(!feof($fd)) |
82 | | | { |
83 | | | $str = fgets($fd,4096); |
84 | | | $str=str_replace("\r", "", $str); |
85 | | | trim($str); |
86 | | | |
87 | | | list($key,$mask) = explode("\t",$str); |
88 | | | if(preg_match("/^(\d+\.\d+\.\d+\.\d+)$/",$mask,$m) && $mask != '0.0.0.0') |
89 | | | { $interfaces[$key]['netmask'] = $m[1]; $count++; } |
90 | | | } |
91 | | | fclose($fd); |
92 | | | } |
93 | | | print "$count netmasks in the cache.\n"; |
94 | | | |
95 | | | print "Collected information on ".sizeof($interfaces)." interfaces and ".sizeof($hosts)." hosts.\n"; |
96 | | | |
97 | | | $cleaned=0; |
98 | | | foreach($interfaces as $key=>$int) |
99 | | | { |
100 | | | if(!isset($int['ip'])) |
101 | | | { |
102 | | | unset($interfaces[$key]); |
103 | | | $cleaned++; |
104 | | | } |
105 | | | else |
106 | | | { |
107 | | | $interfaces[$key]['nicename'] = ( isset($int['name'])?$int['name']:( isset($int['descr'])?$int['descr']: (isset($int['alias'])?$int['alias']:"Interface #".$int['index']) ) ); |
108 | | | } |
109 | | | } |
110 | | | |
111 | | | print "Removed $cleaned interfaces from search, which have no IP address.\n"; |
112 | | | |
113 | | | $count=0; |
114 | | | |
115 | | | foreach($interfaces as $key=>$int) |
116 | | | { |
117 | | | if(!isset($int['netmask'])) |
118 | | | { |
119 | | | $oid = ".1.3.6.1.2.1.4.20.1.3.".$int['ip']; |
120 | | | $hostid = $int['host']; |
121 | | | |
122 | | | if($count<100) |
123 | | | { |
124 | | | print "Fetching Netmask via SNMP for Host ".$int['host']."//".$int['ip']." from $oid\n"; |
125 | | | $result = cacti_snmp_get( |
126 | | | $hosts[$hostid]["hostname"], |
127 | | | $hosts[$hostid]["snmp_community"], |
128 | | | $oid, |
129 | | | $hosts[$hostid]["snmp_version"], |
130 | | | $hosts[$hostid]["snmp_username"], |
131 | | | $hosts[$hostid]["snmp_password"], |
132 | | | $hosts[$hostid]["snmp_auth_protocol"], |
133 | | | $hosts[$hostid]["snmp_priv_passphrase"], |
134 | | | $hosts[$hostid]["snmp_priv_protocol"], |
135 | | | $hosts[$hostid]["snmp_context"], |
136 | | | $hosts[$hostid]["snmp_port"], |
137 | | | $hosts[$hostid]["snmp_timeout"], |
138 | | | SNMP_WEBUI); |
139 | | | if($result != FALSE && preg_match("/^\d+.\d+.\d+.\d+$/",$result)) |
140 | | | { |
141 | | | print "$result|\n"; |
142 | | | $interfaces[$key]['netmask'] = $result; |
143 | | | } |
144 | | | else |
145 | | | { |
146 | | | print "No useful result.\n"; |
147 | | | unset($interfaces[$key]); |
148 | | | } |
149 | | | $count++; |
150 | | | } |
151 | | | |
152 | | | } |
153 | | | } |
154 | | | |
155 | | | $count = 0; |
156 | | | print "Writing Netmask cache...\n"; |
157 | | | $fd = fopen("mapper-cache.txt","w"); |
158 | | | foreach($interfaces as $key=>$int) |
159 | | | { |
160 | | | if(isset($int['netmask'])) |
161 | | | { |
162 | | | fputs($fd,$key."\t".$int['netmask']."\n"); |
163 | | | $count++; |
164 | | | } |
165 | | | } |
166 | | | fclose($fd); |
167 | | | print "Wrote $count cache entries.\n"; |
168 | | | # SNMP netmask => .1.3.6.1.2.1.4.20.1.3.10.1.1.254 |
169 | | | # SNMP interface index => .1.3.6.1.2.1.4.20.1.2.10.1.1.254 |
170 | | | |
171 | | | $count=0; |
172 | | | foreach($interfaces as $key=>$int) |
173 | | | { |
174 | | | if(isset($int['netmask'])) |
175 | | | { |
176 | | | $network = get_network($int['ip'],$int['netmask'])."/".get_cidr($int['netmask']); |
177 | | | $interfaces[$key]['network'] = $network; |
178 | | | |
179 | | | $networks[$network] []= $key; |
180 | | | $count++; |
181 | | | } |
182 | | | else |
183 | | | { |
184 | | | print $int['ip']."\n";; |
185 | | | } |
186 | | | } |
187 | | | print "Assembled $count different network/netmask pairs\n"; |
188 | | | |
189 | | | |
190 | | | $link_config = ""; |
191 | | | $node_config = ""; |
192 | | | $nodes_seen = array(); |
193 | | | |
194 | | | $count=0; |
195 | | | $linkid = 0; |
196 | | | $lannodeid = 0; |
197 | | | foreach ($networks as $network=>$members) |
198 | | | { |
199 | | | if(sizeof($members)<2) |
200 | | | { |
201 | | | unset($networks[$network]); |
202 | | | $count++; |
203 | | | } |
204 | | | |
205 | | | if(sizeof($members)==2) |
206 | | | { |
207 | | | print "Create LINK between\n"; |
208 | | | foreach($members as $int) |
209 | | | { |
210 | | | $h = $interfaces[$int]['host']; |
211 | | | print " ".$interfaces[$int]['nicename']; |
212 | | | print " on ".$hosts[$h]['description']; |
213 | | | print " (".$hosts[$h]['hostname'].")\n"; |
214 | | | $nodes_seen[$h]=1; |
215 | | | } |
216 | | | $linkid++; |
217 | | | $link_config .= "LINK link_$linkid\nWIDTH 4\n"; |
218 | | | $link_config .= "\tNODES node_".$interfaces[$members[0]]['host']." node_".$interfaces[$members[1]]['host']."\n"; |
219 | | | $link_config .= "\tSET in_interface ".$interfaces[$members[1]]['nicename']."\n"; |
220 | | | $link_config .= "\tSET out_interface ".$interfaces[$members[0]]['nicename']."\n"; |
221 | | | $link_config .= "\n"; |
222 | | | } |
223 | | | |
224 | | | if(sizeof($members)>2) |
225 | | | { |
226 | | | print "Create LAN NODE called $network and add LINKs from these NODEs to it:\n"; |
227 | | | $x = rand(0,$width); $y = rand(0,$height); |
228 | | | $lan_key = preg_replace("/[.\/]/","_",$network); |
229 | | | $node_config .= "NODE LAN_$lan_key\nLABELBGCOLOR 255 240 240 \n\tPOSITION $x $y\n\tLABEL $network\n\tICON 96 24 rbox\n\tLABELOFFSET C\n\tLABELOUTLINECOLOR none\nUSESCALE none in\n\n"; |
230 | | | foreach($members as $int) |
231 | | | { |
232 | | | $h = $interfaces[$int]['host']; |
233 | | | print " $int:: ".$interfaces[$int]['nicename']; |
234 | | | print " on ".$hosts[$h]['description']; |
235 | | | print " (".$hosts[$h]['hostname'].")\n"; |
236 | | | $nodes_seen[$h]=1; |
237 | | | $linkid++; |
238 | | | $link_config .= "LINK link_$linkid\n"; |
239 | | | $link_config .= "SET out_interface ".$interfaces[$int]['nicename']."\n"; |
240 | | | $link_config .= "\tNODES node_$h LAN_$lan_key\n\tWIDTH 2\n\tOUTCOMMENT {link:this:out_interface}\n"; |
241 | | | } |
242 | | | print "\n"; |
243 | | | } |
244 | | | } |
245 | | | print "Trimmed $count networks with only one member interface\n"; |
246 | | | |
247 | | | foreach ($nodes_seen as $h=>$c) |
248 | | | { |
249 | | | $x = rand(0,$width); $y = rand(0,$height); |
250 | | | $node_config .= "NODE node_$h\n\tSET cacti_id $h\n"; |
251 | | | $node_config .= "\tLABEL ".$hosts[$h]['description']."\n"; |
252 | | | $node_config .= "\tPOSITION $x $y\n"; |
253 | | | $node_config .= "\tUSESCALE cactiupdown in \n"; |
254 | | | $node_config .= "\tLABELFONTCOLOR contrast\n"; |
255 | | | $node_config .= "\n\n"; |
256 | | | } |
257 | | | |
258 | | | $fd = fopen("automap.cfg","w"); |
259 | | | fputs($fd,"HTMLSTYLE overlib\nBGCOLOR 92 92 92\nWIDTH $width\nHEIGHT $height\nFONTDEFINE 30 GillSans 8\n"); |
260 | | | fputs($fd,"FONTDEFINE 20 GillSans 10\nFONTDEFINE 10 GillSans 9\n"); |
261 | | | fputs($fd,"SCALE DEFAULT 0 0 255 0 0\nSCALE DEFAULT 0 10 32 32 32 0 0 255\nSCALE DEFAULT 10 40 0 0 255 0 255 0\nSCALE DEFAULT 40 55 0 255 0 255 255 0\nSCALE DEFAULT 55 100 240 240 0 255 0 0\n"); |
262 | | | fputs($fd,"\nSCALE cactiupdown 0 0.5 192 192 192 \nSCALE cactiupdown 0.5 1.5 255 0 0 \nSCALE cactiupdown 1.5 2.5 0 0 255 \nSCALE cactiupdown 2.5 3.5 0 255 0 \nSCALE cactiupdown 3.5 4.5 255 255 0 \n"); |
263 | | | fputs($fd,"\nLINK DEFAULT\nBWSTYLE angled\nBWLABEL bits\nBWFONT 30\nCOMMENTFONT 30\n\n"); |
264 | | | fputs($fd,"\nNODE DEFAULT\nLABELFONT 10\n\n"); |
265 | | | fputs($fd,$node_config); |
266 | | | fputs($fd,$link_config); |
267 | | | fclose($fd); |
268 | | | |
269 | | | /////////////////////////////////////////////////////////////// |
270 | | | |
271 | | | function ip_to_int($_ip) |
272 | | | { |
273 | | | if(preg_match("/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/",$_ip,$matches)) |
274 | | | { |
275 | | | $_output=0; |
276 | | | for($i=1;$i<5;$i++) |
277 | | | { |
278 | | | $_output<<=8; |
279 | | | $_output+=$matches[$i]; |
280 | | | } |
281 | | | return($_output); |
282 | | | } |
283 | | | else |
284 | | | { |
285 | | | print "Something funny: $_ip\n"; |
286 | | | return(-1); |
287 | | | } |
288 | | | } |
289 | | | |
290 | | | function int_to_ip($_int) |
291 | | | { |
292 | | | $tmp=$_int; |
293 | | | |
294 | | | for ($i=0; $i < 4; $i++) |
295 | | | { |
296 | | | $IPBit[]=($tmp & 255); |
297 | | | $tmp>>=8; |
298 | | | } |
299 | | | |
300 | | | $_output=sprintf("%d.%d.%d.%d", $IPBit[3], $IPBit[2], $IPBit[1], $IPBit[0]); |
301 | | | return ($_output); |
302 | | | } |
303 | | | |
304 | | | function get_network($_ip, $_mask) |
305 | | | { |
306 | | | $_int1=ip_to_int($_ip); |
307 | | | $_mask1=ip_to_int($_mask); |
308 | | | |
309 | | | $_network=$_int1 & ($_mask1); |
310 | | | |
311 | | | return (int_to_ip($_network)); |
312 | | | } |
313 | | | |
314 | | | function get_cidr($mask) |
315 | | | { |
316 | | | $lookup = array( |
317 | | | "255.255.255.255"=>"32", |
318 | | | "255.255.255.254"=>"31", |
319 | | | "255.255.255.252"=>"30", |
320 | | | "255.255.255.248"=>"29", |
321 | | | "255.255.255.240"=>"28", |
322 | | | "255.255.255.224"=>"27", |
323 | | | "255.255.255.192"=>"26", |
324 | | | "255.255.255.128"=>"25", |
325 | | | "255.255.255.0"=>"24", |
326 | | | "255.255.254.0"=>"23", |
327 | | | "255.255.252.0"=>"22", |
328 | | | "255.255.248.0"=>"21", |
329 | | | "255.255.240.0"=>"20", |
330 | | | "255.255.224.0"=>"19", |
331 | | | "255.255.192.0"=>"18", |
332 | | | "255.255.128.0"=>"17", |
333 | | | "255.255.0.0"=>"16", |
334 | | | "255.254.0.0"=>"15", |
335 | | | "255.252.0.0"=>"14", |
336 | | | "0.0.0.0.0"=>"0" |
337 | | | ); |
338 | | | |
339 | | | if($lookup[$mask]) return ($lookup[$mask]); |
340 | | | |
341 | | | print "HUH: $mask\n"; |
342 | | | |
343 | | | return("-1"); |
344 | | | |
345 | | | |
346 | | | } |
347 | | | |
348 | | | |
349 | | | ?> |