"Fossies" - the Fresh Open Source Software Archive 
Member "node-runner-0.6.0/node.start" (20 Dec 2004, 21838 Bytes) of package /linux/www/old/node-runner-0.6.0.tar.gz:
As a special service "Fossies" has tried to format the requested text file into HTML format (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
1 # Note: Removed php line because I'm tired of answering questions about why it's different on some machines.
2 <?
3
4 $path_to_etc = "etc/"; // Change this as needed. (Remember trailing slash)
5
6
7 # ---------- DO NOT MODIFY BELOW THIS LINE ----------- #
8
9 require_once($path_to_etc."nr.inc");
10
11 function list_of_includes($dir) {
12 // Returns array of file names from $dir
13
14 $i=0;
15 if ($use_dir = @opendir($dir)) {
16 while (($file = readdir($use_dir)) != false) {
17 if (ereg("query_",$file)) {
18 $includes_arr[$i] = "$file";
19 $i++;
20 }
21 }
22 closedir($use_dir);
23 }
24
25 return $includes_arr;
26 }
27
28 $query_type_includes = list_of_includes($path_to_include);
29 $queries_type_array = array();
30 foreach ($query_type_includes as $types) {
31 include_once($path_to_include.$types);
32 }
33
34
35 if ($debug == 1) { $start_time = mktime(); }
36 switch (strtolower($dbtype)) {
37 case ("mysql"):
38 require ($path_to_etc."mysql.inc");
39 break;
40 /* //not implemented yet
41 case ("postgresql"):
42 require ($path_to_etc."postgres.inc");
43 break;
44 */ //not implemented yet
45 }
46
47 function count_deps($parent) {
48 GLOBAL $cnt;
49 // Recursive function to count dependencies
50 // Used for detailed email alerts
51 $sql = "SELECT id FROM objects WHERE dependency='$parent'";
52 $result = db_query($sql);
53 while ($r = db_fetch_array($result)) {
54 $cnt++;
55 count_deps($r['id']);
56 }
57 return $cnt;
58 }
59
60 function build_avoid_polling($node_id, $node_description) {
61 global $dont_poll;
62 // Recursive function to backtrace dependencies
63 $dont_poll[$node_id] = $node_description;
64
65 $sql = "SELECT id,description FROM objects WHERE dependency='$node_id'";
66 $result = db_query($sql);
67 while ($r = db_fetch_array($result)) {
68 $id = $r["id"];
69 $description = $r["description"];
70 $dont_poll[$id] = $description;
71 build_avoid_polling($id, $description);
72 }
73
74 return $dont_poll;
75 }
76
77 function mailer($id, $mail_id, $status, $status_msg, $now, $sender, $downtime) {
78 GLOBAL $detailed_email,$qtime;
79 $query = "SELECT email FROM mail_group WHERE id='$mail_id'";
80 $result = db_query($query);
81 while ($r = db_fetch_array($result)) {
82 $email=$r["email"];
83
84 $query_comments = "SELECT description,ipaddress,port,query_type,comments FROM objects WHERE id='$id'";
85 $result_comments = db_query($query_comments);
86 list($id_description,$id_ipaddress,$id_port,$id_query_type,$id_comments) = db_fetch_array($result_comments);
87 if ($detailed_email == 1) {
88
89 if ($id_comments) { $comments = "Comments: ".$id_comments; }
90 $query_affected = "SELECT description FROM objects WHERE dependency='$id'";
91 $result_affected = db_query($query_affected);
92 $num_rows_affected = db_num_rows($result_affected);
93 unset($dir_affected);
94 if ($num_rows_affected == 0) {
95 $dir_affected = "NONE";
96 $affected_cnt = 0;
97 } else {
98 $affected_cnt = count_deps($id);
99 $dir_affected = "\n";
100 while ($s = db_fetch_array($result_affected)) {
101 $each_affected = $s["description"];
102 $dir_affected .= $each_affected."\n";
103 }
104 }
105
106 if (($id_query_type == 'SNMP') || ($id_query_type == 'ICMP')) {
107 $id_port_info = $id_query_type;
108 } else {
109 $id_port_info = $id_query_type.' Port: '. $id_port;
110 }
111
112 if (!$downtime) {
113 $downtime = "Less than $qtime minute(s)";
114 } else {
115 if ($downtime < 60) {
116 $downtime = "$downtime Second(s)";
117 } elseif (($downtime >= 60) && ($downtime < 3600)) {
118 $downtime = round($downtime/60, 2) . " Minute(s)";
119 } else {
120 $downtime = round($downtime/3600, 2) . " Hour(s)";
121 }
122 }
123
124 if (!$status) {
125 mail("$email","$id_description DOWN","$status_msg\nIP: $id_ipaddress - $id_port_info\n\n$now\n\nDowntime: $downtime\n\nNodes Directly Affected: $dir_affected\nTotal Nodes Affected: $affected_cnt\n\n$comments","From:$sender");
126 } else {
127 // Send a different message if up - we don't need as much info when it comes back up.
128 mail("$email","$id_description UP","$status_msg\n$now\n\nTotal Downtime: $downtime","From:$sender");
129 }
130
131 } else {
132 if (!$status) {
133 $subject = $id_description." DOWN";
134 } else {
135 $subject = $id_description." UP";
136 }
137 mail("$email","$subject","$status_msg\n$now","From:$sender");
138 }
139 }
140 }
141
142
143 function query_socket($description, $ipaddress, $port, $query_type, $snmp_comm, $url, $username, $pass, $ptime, $max_attempts) {
144 GLOBAL $debug,$allow_refused;
145 $i=0;
146 do {
147 if ($debug == 1) { echo "Attempting query #".intval($i+1)." of $description...\n"; } //debugging
148 if ($i > 0) { sleep(3); }
149 switch($query_type) {
150 case "HTTP":
151 $stat = http_query($description, $ipaddress, $port, $url, $username, $pass, $ptime);
152 break;
153 case "ICMP":
154 $stat = icmp_query($description, $ipaddress, $ptime);
155 break;
156 case "TCP":
157 $stat = tcp_query($description, $ipaddress, $port, $ptime);
158 break;
159 case "SNMP":
160 $stat = snmp_query($description, $ipaddress, $snmp_comm, $ptime);
161 break;
162 case "UDP":
163 $stat = udp_query($description, $ipaddress, $port, $ptime);
164 break;
165 }
166 $i++;
167 } while (($i < $max_attempts) && (!$stat[0]));
168 return $stat;
169 }
170
171 db_connect();
172
173
174 //Make sure dont_poll array is empty to start script
175 $dont_poll = array();
176
177
178 // Used for identifying resurrected servers
179 $al_time = mktime();
180 $al_now = date("m-d-Y g:i:s a", $al_time);
181 $query9 = "SELECT * FROM alert_log WHERE resolved='N'";
182 $result9 = db_query($query9);
183 while ($r = db_fetch_array($result9)) {
184 $al_id = $r["id"];
185 $al_description = $r["description"];
186 if ((!empty($dont_poll)) && (in_array($al_description, $dont_poll))) {
187 continue; // Don't poll this node if we've already determined that a higher-level parent node is down.
188 }
189 $al_ipaddress = $r["ipaddress"];
190
191 // Query ID from 'objects' table
192 $query_real_id = "SELECT id,enabled FROM objects WHERE description='".$al_description."' AND ipaddress='".$al_ipaddress."'";
193 $result_real_id = db_query($query_real_id);
194 list($objects_id,$objects_enabled) = db_fetch_array($result_real_id);
195
196 $al_dependency = $r["dependency"];
197 $al_port = $r["port"];
198 $al_query_type = $r["query_type"];
199 $al_snmp_comm = $r["snmp_comm"];
200 $al_start_time = $r["time"];
201 $al_downtime = $r["downtime"];
202 $al_lastnotif = $r["lastnotif"];
203 $al_ptime = $r["ptime"];
204 $al_url = $r["url"];
205 $al_user = $r["auth_user"];
206 $al_pass = $r["auth_pass"];
207 $query10 = "SELECT mail_group FROM objects WHERE description='$al_description' AND dependency='$al_dependency'";
208 $result10 = db_query($query10);
209 list($al_mail_group) = db_fetch_array($result10);
210
211 // 'orphans' a log entry if node becomes unreachable during dependent server's downtime
212 $dep_query1 = "SELECT description,ipaddress,port,query_type,snmp_comm,url,ptime,auth_user,auth_pass FROM objects WHERE id='$al_dependency'";
213 $dep_result1 = db_query($dep_query1);
214 list($parents_description,$parents_ipaddress,$parents_port,$parents_query_type,$parents_snmp_comm,$parents_url,$parents_ptime,$parents_auth_user,$parents_auth_pass) = db_fetch_array($dep_result1);
215 $dep_query2 = db_query("SELECT id FROM alert_log WHERE description='$parents_description' AND resolved='N'");
216 if(db_num_rows($dep_query2) == 1) {
217 $query12 = "UPDATE alert_log SET resolved='O' WHERE id='$al_id'";
218 $result12 = db_query($query12);
219 if ($result12 == 1) {
220 unset($result12);
221 continue;
222 }
223 }
224
225 // 'orphans' a log entry if node is manually disabled by admin during its own downtime
226 if ($objects_enabled == 'N') {
227 $query12 = "UPDATE alert_log SET resolved='O' WHERE id='$al_id'";
228 $result12 = db_query($query12);
229 if ($result12 == 1) {
230 unset($result12);
231 continue;
232 }
233 }
234
235 $connection1 = query_socket($al_description, $al_ipaddress, $al_port, $al_query_type, $al_snmp_comm, $al_url, $al_user, $al_pass, $al_ptime, 1);
236
237 if ($connection1[0]) { // if server is back UP...
238 unset($al_stat,$al_stat_msg);
239 $al_stat = $connection1[0];
240 $al_stat_msg = $connection1[1];
241 $al_time = mktime(); //update the time
242 $al_newtime = $al_time - $al_start_time;
243
244 if ($al_downtime == 0) {
245 $al_mailer_time = $al_newtime;
246 $query11 = "UPDATE alert_log SET downtime='$al_newtime',lastnotif='$al_time',resolved='Y' WHERE id='$al_id'";
247 } else {
248 $al_mailer_time = $al_downtime;
249 $query11 = "UPDATE alert_log SET lastnotif='$al_time',resolved='Y' WHERE id='$al_id'";
250 }
251 $result11 = db_query($query11);
252
253 if ($firstmail == 0) { // If $firstmail is zero, we know that a DOWN message has already been sent when the node first went offline.
254 $out = mailer($objects_id, $al_mail_group, $al_stat, $al_stat_msg, $al_now, $sender, $al_mailer_time);
255 } else if (($firstmail>0) && ($al_start_time != $al_lastnotif)) { // If $firstmail is NOT zero, we need to make sure we've been notified at least once before of the DOWN status before we send an UP status.
256 $out = mailer($objects_id, $al_mail_group, $al_stat, $al_stat_msg, $al_now, $sender, $al_mailer_time);
257 }
258
259 if ($debug == 1) { //Used for debugging
260 echo $al_stat_msg;
261 echo "$al_description came back up.\n";
262 } //end debugging
263
264
265
266
267 // Insert entry into dont_poll array to avoid polling later in this script.
268 $dont_poll[$objects_id] = $al_description;
269 //Used for debugging
270 if (($debug == 1) && (!empty($dont_poll))) {
271 echo "As a result, $al_description will not be polled again during this iteration.\n\n";
272 }
273
274 } else { // if server is NOT back up yet...
275
276 // First test parent, maybe it's the parent's fault...
277 if ($debug == 1) { //Used for debugging
278 echo "Checking parent just in case...\n";
279 } // end debugging
280
281 $connection1a = query_socket($parents_description, $parents_ipaddress, $parents_port, $parents_query_type, $parents_snmp_comm, $parents_url, $parents_user, $parents_pass, $parents_ptime, 1);
282 if (!$connection1a[0]) { // if parent is down, change resolved status to 'O'
283 $parents_stat_msg = $connection1a[1];
284
285 $query_orphan1 = db_query("UPDATE alert_log SET resolved='O' WHERE id='$al_id'");
286
287 // Note that we don't make an alert_log entry for the down parent at this point.
288 // We'll let NR do that during its process of querying normal nodes.
289
290 if ($debug == 1) { //Used for debugging
291 echo $parents_stat_msg;
292 echo "$al_description has been orphaned because $parents_description is now down.\n\n";
293 } //end debugging
294
295 continue; // Skip the rest - if we've orphaned this node, we don't need to send a mail or modify it any further.
296 } else if (($connection1a[0]) && ($debug == 1)) {
297 echo "Parent appears to be up, so that's not the problem. Moving on...\n\n";
298 }
299
300 $al_stat_msg = $connection1[1];
301 $al_time = mktime(); //update the time
302 $al_newtime = $al_time - $al_start_time;
303 $al_mailer_time = $al_newtime;
304
305 if ($al_downtime == 0) {
306 $al_newtime = ($qtime * 60);
307 $query11 = "UPDATE alert_log SET downtime='$al_newtime' WHERE id='$al_id'";
308 } else {
309 $al_newtime = ((intval($al_downtime)) + ($qtime * 60));
310 $query11 = "UPDATE alert_log SET downtime='$al_newtime' WHERE id='$al_id'";
311 }
312 $result11 = db_query($query11);
313
314 $chktime = intval(mktime() - $al_start_time);
315 if (($firstmail>0) && ($al_start_time == $al_lastnotif) && ((($chktime/60) >= $firstmail))) {
316 // We need to make sure we wait at least $firstmail minutes before sending the first mail.
317 $query8a = "UPDATE alert_log SET lastnotif='$al_time' WHERE id='$al_id'";
318 $result8a = db_query($query8a);
319 $out = mailer($objects_id, $al_mail_group, $al_stat, $al_stat_msg, $al_now, $sender, $al_mailer_time);
320 } else if ($al_start_time != $al_lastnotif) {
321 // Since lastnotif has been updated, we know that the first email has already been sent. Now check to see if we need to send additional emails.
322 if (($al_time - ($delay * 60)) > $al_lastnotif) {
323 $query8a = "UPDATE alert_log SET lastnotif='$al_time' WHERE id='$al_id'";
324 $result8a = db_query($query8a);
325 if ($max_alerts == 0) { // Send unlimited email notifications in $delay increments until node comes back up.
326 $out = mailer($objects_id, $al_mail_group, $al_stat, $al_stat_msg, $al_now, $sender, $al_mailer_time);
327 } else if (($al_lastnotif - $al_start_time) <= (($delay * 60) * $max_alerts)) {
328 $out = mailer($objects_id, $al_mail_group, $al_stat, $al_stat_msg, $al_now, $sender, $al_mailer_time);
329 }
330 }
331 }
332
333 if ($debug == 1) { //Used for debugging
334 echo $al_stat_msg;
335 echo "$al_description is still down.\n";
336 } //end debugging
337
338 // Insert entry into dont_poll array to avoid polling later in this script.
339 $dont_poll = build_avoid_polling($objects_id, $al_description);
340 //Used for debugging
341 if (($debug == 1) && (!empty($dont_poll))) {
342 echo "$al_description nor it dependents will be polled again during this iteration.\n\n";
343 } //end debugging
344
345 }
346
347 }
348
349 // Begin recursive queries for down servers
350 $time_x = mktime();
351 $date_x = intval(date("Hi", $time_x));
352 $today = date("D", $time_x);
353 $query1 = "SELECT * FROM objects WHERE server='Y' and enabled='Y' and smon_time<='$date_x' and emon_time>='$date_x' and days like '%$today%' ORDER BY description ASC";
354 $result1 = db_query($query1);
355 while ($r = db_fetch_array($result1)) {
356 $id_x = $r["id"];
357 $dependency_x = $r["dependency"];
358 $description_x = $r["description"];
359 $ipaddress_x = $r["ipaddress"];
360 $port_x = $r["port"];
361 $query_type_x = $r["query_type"];
362 $snmp_comm_x = $r["snmp_comm"];
363 $server_x = $r["server"];
364 $mail_group_x = $r["mail_group"];
365 $ptime_x = $r["ptime"];
366 $url_x = $r["url"];
367 $user_x = $r["auth_user"];
368 $pass_x = $r["auth_pass"];
369
370 if ((!empty($dont_poll)) && (in_array($description_x, $dont_poll))) {
371 continue;
372 }
373
374 $connection2 = query_socket($description_x, $ipaddress_x, $port_x, $query_type_x, $snmp_comm_x, $url_x, $user_x, $pass_x, $ptime_x, $max_attempts);
375
376 if (!$connection2[0]) {
377 // This one is down too, try the next node in the chain (ping3)
378
379 unset($stat,$stat_msg);
380 $stat = $connection2[0];
381 $stat_msg = $connection2[1];
382
383 if ($debug == 1) { //Used for debugging
384 unset($output);
385 //$output = $description_x." unreachable, trying parent...\n";
386 $output = $connection2[1];
387 $output .= $description_x." unreachable, trying parent...\n\n";
388 echo $output;
389 } //end debugging
390
391 $recursion = $dependency_x;
392
393 $n=0; //node counter
394 do {
395 $query2 = "SELECT * FROM objects WHERE id='$recursion'";
396 $result2 = db_query($query2);
397 while ($r = db_fetch_array($result2)) {
398 $id = $r["id"];
399 $dependency = $r["dependency"];
400 $description = $r["description"];
401 $ipaddress = $r["ipaddress"];
402 $port = $r["port"];
403 $query_type = $r["query_type"];
404 $snmp_comm = $r["snmp_comm"];
405 $server = $r["server"];
406 $mail_group = $r["mail_group"];
407 $ptime = $r["ptime"];
408 $url = $r["url"];
409 $user = $r["auth_user"];
410 $pass = $r["auth_pass"];
411
412 $connection3 = query_socket($description, $ipaddress, $port, $query_type, $snmp_comm, $url, $user, $pass, $ptime, $max_attempts);
413
414 if (!$connection3[0]) {
415 // This one is down too. Set $recursion to next parent level up and try that one.
416
417 unset($stat,$stat_msg);
418 $stat = $connection3[0];
419 $stat_msg = $connection3[1];
420
421 $recursion = $dependency;
422
423 // The following values are generated to identify the failing dependent
424 $return_id = $id;
425 $return_dependency = $dependency;
426 $return_description = $description;
427 $return_ipaddress = $ipaddress;
428 $return_port = $port;
429 $return_query_type = $query_type;
430 $return_server = $server;
431 $return_mail_group = $mail_group;
432 $return_ptime = $ptime;
433 $return_url = $url;
434 $return_snmp_comm = $snmp_comm;
435
436 if ($debug == 1) { //Used for debugging
437 unset($output);
438 //$output = "$description unreachable, trying parent...\n";
439 $output = $connection3[1];
440 $output .= $description." unreachable, trying parent...\n\n";
441 echo $output;
442 } //end debugging
443
444 $end = "N";
445 $n++;
446
447 } else {
448 // This one is up, so it must be its child that is down.
449
450 if ($n < 1) {
451 $return_id = $id_x;
452 $return_dependency = $dependency_x;
453 $return_description = $description_x;
454 $return_ipaddress = $ipaddress_x;
455 $return_port = $port_x;
456 $return_query_type = $query_type_x;
457 $return_server = $server_x;
458 $return_mail_group = $mail_group_x;
459 $return_ptime = $ptime_x;
460 $return_url = $url_x;
461 $return_snmp_comm = $snmp_comm_x;
462 }
463
464 //Used for debugging
465 if ($debug == 1) { echo "$description is up.\nMust be $return_description that's down.\n\n"; }
466 //end debugging
467
468 if (($debug == 1) && (!empty($dont_poll))) {
469 $sizeof_dont_poll_before = sizeof($dont_poll);
470 }
471
472 $dont_poll = build_avoid_polling($return_id, $return_description);
473
474 if (($debug == 1) && (!empty($dont_poll))) {
475 $sizeof_dont_poll_after = sizeof($dont_poll);
476 }
477
478 //Used for debugging
479 if (($debug == 1) && (!empty($dont_poll)) && ($sizeof_dont_poll_after > $sizeof_dont_poll_before)) {
480 echo "As a result, these servers will not be polled again during this iteration:\n";
481 foreach ($dont_poll as $arr_desc) {
482 echo "==> $arr_desc\n";
483 }
484 echo "\n\n";
485 } //end debugging
486
487 //unset($dont_poll);
488
489
490 $time = mktime();
491 $now = date("m-d-Y g:i:s a", $time);
492
493 // The following check was added to ensure that duplicate entries are not created in the case of overlapping query script executions.
494 // This would only occur if the collective polling time for the iteration took longer than $qtime.
495 $already_exist = db_query("SELECT id FROM alert_log WHERE description='$return_description' AND resolved='N'");
496 $num_rows_exist = db_num_rows($already_exist);
497 if (!$num_rows_exist) {
498 if ($return_url) {
499 $query7 = "INSERT INTO alert_log VALUES ('','$return_dependency','$return_description','$return_ipaddress','$return_port','$return_query_type','$return_server','$time',0,'$time','$return_ptime','$return_url',NULL,'N')";
500 } else if ($return_snmp_comm) {
501 $query7 = "INSERT INTO alert_log VALUES ('','$return_dependency','$return_description','$return_ipaddress','$return_port','$return_query_type','$return_server','$time',0,'$time','$return_ptime',NULL,'$return_snmp_comm','N')";
502 } else {
503 $query7 = "INSERT INTO alert_log VALUES ('','$return_dependency','$return_description','$return_ipaddress','$return_port','$return_query_type','$return_server','$time',0,'$time','$return_ptime',NULL,NULL,'N')";
504 }
505 $result7 = db_query($query7);
506
507 if ($firstmail == 0) { // Assume user wants to know immediately, otherwise we'll send a mail as we're checking for resurrected servers in the next iteration.
508 $out = mailer($return_id, $return_mail_group, $stat, $stat_msg, $now, $sender, 0);
509 }
510 }
511
512 $end = "Y";
513 $n++;
514 } // end if
515 } // end while
516 } while ($end != "Y"); // while from the 'do' statement
517
518
519 } else { // debugging
520
521 if ($debug == 1) {
522 unset($output);
523 //$output = "$description_x UP\n";
524 $output = $connection2[1];
525 $output .= "\n";
526 echo $output;
527 } // end debugging
528
529 }
530 } // end while
531
532 if ($debug == 1) {
533 $end_time = mktime();
534 $exec_time = $end_time - $start_time;
535 echo "\n\n";
536 echo ''. date("l n/d/y g:i a", $end_time) .'';
537 echo "\nExecution Time: $exec_time Seconds";
538 echo "\n------------------------------\n\n";
539 }
540 ?>