--- thold/check-thold.php	Sat Jun  2 23:53:05 2007
+++ ../cacti/plugins/thold/check-thold.php	Wed Aug  1 15:54:28 2007
@@ -136,6 +136,9 @@
 
 			if ( $breach_up || $breach_down) {
 				$item["thold_fail_count"]++;
+				$nagios_array["nagios_host"]=$item["nagios_host"];
+                                $nagios_array["nagios_service"]=$item["nagios_service"];
+                                $nagios_array["nagios_state"]=2;
 
 				// Re-Alert?
 				$ra = ($item["thold_fail_count"] > $trigger && ($item["thold_fail_count"] % ($item["repeat_alert"] == "" ? $realert : $item["repeat_alert"])) == 0);
@@ -147,9 +150,9 @@
 					if ($show)
 						print " " . ($ra ? "is still" : "went") . " " . ($breach_up ? "above" : "below") . " threshold of " . ($breach_up ? $item["thold_hi"] : $item["thold_low"]) . " with $currentval\n";
 					if ($global_notify_enabled || $item["notify_default"] != "off")
-						thold_mail($global_alert_address, '', $subject, $msg, $file_array);
+						thold_mail($global_alert_address, '', $subject, $msg, $file_array, '', $nagios_array);
 					if (trim($item["notify_extra"]) != "")
-						thold_mail($item["notify_extra"], '', $subject, $msg, $file_array);
+						thold_mail($item["notify_extra"], '', $subject, $msg, $file_array, '', $nagios_array);
 				} elseif ($show) {
 						print " " . ($ra ? "is still" : "went") . " " . ($breach_up ? "above" : "below") . " threshold of " . ($breach_up ? $item["thold_hi"] : $item["thold_low"]) . " with $currentval\n";
 				}
@@ -162,6 +165,10 @@
 			
 			} else {
 				if ($alertstat != 0) {
+                                        $nagios_array["nagios_host"]=$item["nagios_host"];
+                                        $nagios_array["nagios_service"]=$item["nagios_service"];
+                                        $nagios_array["nagios_state"]=0;
+
 					if ($logset == 1)
 						logger($desc, "ok", 0, $currentval, $trigger, $item["thold_fail_count"]);
 					if ($item["thold_fail_count"] >= $trigger) {
@@ -169,9 +176,9 @@
 						if ($show)
 							print " restored to normal threshold with value $currentval\n";
 						if ($global_notify_enabled || $item["notify_default"] != "off")
-							thold_mail($global_alert_address, '', $subject, $msg, $file_array);
+							thold_mail($global_alert_address, '', $subject, $msg, $file_array, '', $nagios_array);
 						if (trim($item["notify_extra"]) != "")
-							thold_mail($item["notify_extra"], '', $subject, $msg, $file_array);
+							thold_mail($item["notify_extra"], '', $subject, $msg, $file_array, '', $nagios_array);
 					} elseif ($show) {
 						print "\n";
 					}
@@ -257,4 +264,4 @@
 	}
 }
 
-?>
+?>

--- thold/setup.php	Sat Jun  2 23:53:05 2007
+++ ../cacti/plugins/thold/setup.php	Wed Aug  1 15:56:04 2007
@@ -629,7 +629,24 @@
 			"method" => "textbox_password",
 			"max_length" => 255,
 			),
+                "thold_nagios_header" => array(
+                        "friendly_name" => "Nagios Notification Options",
+                        "method" => "spacer",
+                        ),
+                "thold_nagios_notification" => array(
+                        "friendly_name" => "Send notifications to Nagios",
+                        "description" => "Enable sending alert notification to Nagios for SMS/email alerting",
+                        "method" => "checkbox",
+                        "default" => "on"
+                        ),
+                "thold_nagios_script" => array(
+                        "friendly_name" => "Nagios Script",
+                        "description" => "This is the script (with path) to execute when alerting to Nagios. The script must be executable by the user running the HTTP process. This user must also have write permission o
+n the Nagios external command file. ex : /opt/scripts/cacti-to-nagios.sh",
+                        "method" => "textbox",
+                        "max_length" => 255,
+                        ),
 		);
 }
 
-?>
+?>

--- thold/thold-functions.php	Sat Jun  2 23:53:05 2007
+++ ../cacti/plugins/thold/thold-functions.php	Thu Aug  2 10:37:14 2007
@@ -73,6 +73,10 @@
 		return;
 	$item = $item[0];
 
+        $nagios_array["nagios_host"]=$item["nagios_host"];
+        $nagios_array["nagios_service"]=$item["nagios_service"];
+        $nagios_array["nagios_state"]=2;
+
 	if ($cdef != 0)
 		$currentval = thold_build_cdef($cdef, $currentval, $rra_id, $data_id);
 	$currentval = round($currentval, 4);
@@ -132,12 +136,13 @@
 				logger($desc, $breach_up, ($breach_up ? $item["thold_hi"] : $item["thold_low"]), $currentval, $trigger, $item["thold_fail_count"]);
 			}
 			$subject = $desc . ($thold_show_datasource ? " [$name]" : '') . " " . ($ra ? "is still" : "went") . " " . ($breach_up ? "above" : "below") . " threshold of " . ($breach_up ? $item["thold_hi"] : $item["thold_low"]) . " with $currentval";
+			$nagios_array["nagios_state"]=2;
 			if ($show)
 				print " " . ($ra ? "is still" : "went") . " " . ($breach_up ? "above" : "below") . " threshold of " . ($breach_up ? $item["thold_hi"] : $item["thold_low"]) . " with $currentval\n";
 			if (($global_notify_enabled && $item["notify_default"] != "off") || $item["notify_default"] == "on" )
-				thold_mail($global_alert_address, '', $subject, $msg, $file_array);
+				thold_mail($global_alert_address, '', $subject, $msg, $file_array, '', $nagios_array);
 			if (trim($item["notify_extra"]) != "")
-				thold_mail($item["notify_extra"], '', $subject, $msg, $file_array);
+				thold_mail($item["notify_extra"], '', $subject, $msg, $file_array, '', $nagios_array);
 		} elseif ($show) {
 				print " " . ($ra ? "is still" : "went") . " " . ($breach_up ? "above" : "below") . " threshold of " . ($breach_up ? $item["thold_hi"] : $item["thold_low"]) . " with $currentval\n";
 		}
@@ -153,12 +158,13 @@
 				logger($desc, "ok", 0, $currentval, $trigger, $item["thold_fail_count"]);
 			if ($item["thold_fail_count"] >= $trigger) {
 				$subject = $desc . ($thold_show_datasource ? " [$name]" : '') . " restored to normal threshold with value $currentval";
+				$nagios_array["nagios_state"]=0;
 				if ($show)
 					print " restored to normal threshold with value $currentval\n";
 				if (($global_notify_enabled && $item["notify_default"] != "off") || $item["notify_default"] == "on" )
-					thold_mail($global_alert_address, '', $subject, $msg, $file_array);
+					thold_mail($global_alert_address, '', $subject, $msg, $file_array, '', $nagios_array);
 				if (trim($item["notify_extra"]) != "")
-					thold_mail($item["notify_extra"], '', $subject, $msg, $file_array);
+					thold_mail($item["notify_extra"], '', $subject, $msg, $file_array, '', $nagios_array);
 			} elseif ($show) {
 				print "\n";
 			}
@@ -185,12 +191,13 @@
 				case 0:		// All clear
 					if ($global_bl_notify_enabled && $item["bl_fail_count"] >= $bl_fail_trigger) {
 						$subject = $desc . ($thold_show_datasource ? " [$name]" : '') . " restored to normal threshold with value $currentval";
+						$nagios_array["nagios_state"]=0;
 						if ($show)
 							print " restored to normal threshold with value $currentval\n";
 						if (($global_notify_enabled && $item["notify_default"] != "off") || $item["notify_default"] == "on" )
-							thold_mail($global_alert_address, '', $subject, $msg, $file_array);
+							thold_mail($global_alert_address, '', $subject, $msg, $file_array, '', $nagios_array);
 						if (trim($item["notify_extra"]) != "")
-							thold_mail($item["notify_extra"], '', $subject, $msg, $file_array);
+							thold_mail($item["notify_extra"], '', $subject, $msg, $file_array, '', $nagios_array);
 					}
 					$item["bl_fail_count"] = 0;
 					break;
@@ -206,12 +213,13 @@
 							logger($desc, $breach_up, ($breach_up ? $item["thold_hi"] : $item["thold_low"]), $currentval, $item["thold_fail_trigger"], $item["thold_fail_count"]);
 						}
 						$subject = $desc . ($thold_show_datasource ? " [$name]" : '') . " " . ($ra ? "is still" : "went") . " " . ($item["bl_alert"] == 2 ? "above" : "below") . " calculated baseline threshold with $currentval";
+						$nagios_array["nagios_state"]=2;
 						if ($show)
 							print " " . ($ra ? "is still" : "went") . " " . ($item["bl_alert"] == 2 ? "above" : "below") . " calculated baseline threshold with $currentval\n";;
 						if (($global_notify_enabled && $item["notify_default"] != "off") || $item["notify_default"] == "on" )
-							thold_mail($global_alert_address, '', $subject, $msg, $file_array);
+							thold_mail($global_alert_address, '', $subject, $msg, $file_array, '', $nagios_array);
 						if (trim($item["notify_extra"]) != "")
-							thold_mail($item["notify_extra"], '', $subject, $msg, $file_array);
+							thold_mail($item["notify_extra"], '', $subject, $msg, $file_array, '', $nagios_array);
 					}
 					break;
 			}
@@ -243,7 +251,7 @@
 
 function logger($desc, $breach_up, $threshld, $currentval, $trigger, $triggerct) {
 	define_syslog_variables();
-	openlog("CactiTholdLog", LOG_PID | LOG_PERROR, LOG_LOCAL0);
+	openlog("CactiTholdLog", LOG_PID|LOG_PERROR, LOG_LOCAL0);
 
 	$syslog_level = read_config_option('thold_syslog_level');
 	if (!isset($syslog_level)) {
@@ -593,7 +601,7 @@
 
         $existing = db_fetch_assoc("SELECT id FROM thold_data WHERE rra_id = " . $rra . " AND data_id = " . $_POST["data_template_rrd_id"]);
         if (count($existing) == 0) {
-                $sql = "INSERT INTO thold_data (host_id, rra_id, data_id, thold_hi, thold_low, thold_fail_trigger, thold_enabled, bl_enabled, repeat_alert, notify_default, notify_extra, cdef";
+                $sql = "INSERT INTO thold_data (host_id, rra_id, data_id, thold_hi, thold_low, thold_fail_trigger, thold_enabled, bl_enabled, repeat_alert, notify_default, notify_extra, cdef, nagios_host, nagios_service";
                 if($_POST["bl_enabled"] == "on") {
                         $sql .= ", bl_ref_time, bl_ref_time_range, bl_pct_down, bl_pct_up, bl_fail_trigger";
                 }
@@ -609,6 +617,8 @@
                 $sql .= ", " . ($_POST["notify_default"] == "NULL" ? "NULL" : ("'" . $_POST["notify_default"] . "'"));
                 $sql .= ", " . (trim($_POST["notify_extra"]) == "" ? "NULL" : ("'" . $_POST["notify_extra"] . "'"));
                 $sql .= ", " . $_POST["cdef"];
+                $sql .= ", " . (trim($_POST["nagios_host"]) == "" ? "NULL" : ("'" . $_POST["nagios_host"] . "'"));
+                $sql .= ", " . (trim($_POST["nagios_service"]) == "" ? "NULL" : ("'" . $_POST["nagios_service"] . "'"));
                 if($_POST["bl_enabled"] == "on") {
                         $sql .= ", '" . $_POST["bl_ref_time"] . "'";
                         $sql .= ", '" . $_POST["bl_ref_time_range"] . "'";
@@ -630,6 +640,8 @@
                 $sql .= ", notify_extra=" . (trim($_POST["notify_extra"]) == "" ? "NULL" : ("'" . $_POST["notify_extra"] . "'"));
                 $sql .= ", template_enabled='" . $_POST["template_enabled"] . "'";
                 $sql .= ", cdef=" . $_POST["cdef"];
+                $sql .= ", nagios_host=" . (trim($_POST["nagios_host"]) == "" ? "NULL" : ("'" . $_POST["nagios_host"] . "'"));
+                $sql .= ", nagios_service=" . (trim($_POST["nagios_service"]) == "" ? "NULL" : ("'" . $_POST["nagios_service"] . "'"));
                 if($_POST["bl_enabled"] == "on") {
                         $sql .= ", bl_ref_time='" . $_POST["bl_ref_time"] . "'";
                         $sql .= ", bl_ref_time_range='" . $_POST["bl_ref_time_range"] . "'";
@@ -760,7 +772,7 @@
 
 /* Sends a group of graphs to a user */
 
-function thold_mail($to, $from, $subject, $message, $filename, $headers = '') {
+function thold_mail($to, $from, $subject, $message, $filename, $headers = '', $nagios_array='') {
 	global $config;
 	include_once($config["base_path"] . "/plugins/thold/class.phpmailer.php");
 	$mail = new PHPMailer();
@@ -863,7 +875,19 @@
 		$mail->AltBody = strip_tags(str_replace('<br>', "\n", $message));
 	}
 
+	// added by prune - 20070801
+	// push the state value to an external script for nagios passive check
 
+	// check the use of nagion is enabled
+	if (is_array($nagios_array) && read_config_option("thold_nagios_notification") == "on") {
+        	$nagios_msg="[".time()."] PROCESS_SERVICE_CHECK_RESULT;".$nagios_array["nagios_host"].";".$nagios_array["nagios_service"].";".$nagios_array["nagios_state"].";".escapeshellcmd($subject);
+
+		// run an external command - this is not pretty but this way we have no link between cacti and the other software (nagios)
+		exec ( read_config_option("thold_nagios_script").' "'.$nagios_msg.'"');
+		}
+	// done
+
+
 	if(!$mail->Send()) {
 		return $mail->ErrorInfo;
 	}
@@ -871,6 +895,7 @@
 	if ($mail->ErrorInfo != '')
 		return $mail->ErrorInfo;
 	return '';
+
 }
 
 function thold_template_update_threshold ($id, $template) {

--- thold/thold.php	Sat Jun  2 23:53:06 2007
+++ ../cacti/plugins/thold/thold.php	Wed Aug  1 15:57:22 2007
@@ -371,6 +371,26 @@
 			"description" => "You may specify here extra e-mails to receive alerts for this data source (comma separated)",
 			"value" => isset($thold_item_data["notify_extra"]) ? $thold_item_data["notify_extra"] : ""
 		),
+                "nagios_header" => array(
+                        "friendly_name" => "Nagios Alerts",
+                        "method" => "spacer",
+                ),
+
+                "nagios_host" => array(
+                        "friendly_name" => "Nagios Hostname",
+                        "method" => "textbox",
+                        "max_length" => 255,
+                        "description" => "The hotsname of the server holding the service in Nagios",
+                        "value" => isset($thold_item_data["nagios_host"]) ? $thold_item_data["nagios_host"] : ""
+                ),
+
+                "nagios_service" => array(
+                        "friendly_name" => "Nagios Service Name",
+                        "method" => "textbox",
+                        "max_length" => 255,
+                        "description" => "The name of the Service defined in Nagios",
+                        "value" => isset($thold_item_data["nagios_service"]) ? $thold_item_data["nagios_service"] : ""
+                ),
 		
 	);
 
@@ -461,4 +481,4 @@
 
 document.THold.element.onchange = GraphImage;
 
-</script>
+</script>

--- thold/thold_templates.php	Sat Jun  2 23:53:06 2007
+++ ../cacti/plugins/thold/thold_templates.php	Wed Aug  1 15:59:14 2007
@@ -489,6 +489,27 @@
 			"description" => "You may specify here extra e-mails to receive alerts for this data source (comma separated)",
 			"value" => isset($thold_item_data["notify_extra"]) ? $thold_item_data["notify_extra"] : ""
 		),
+
+                "nagios_header" => array(
+                        "friendly_name" => "Nagios Alerts",
+                        "method" => "spacer",
+                ),
+
+                "nagios_host" => array(
+                        "friendly_name" => "Nagios Hostname",
+                        "method" => "textbox",
+                        "max_length" => 255,
+                        "description" => "The hotsname of the server holding the service in Nagios",
+                        "value" => isset($thold_item_data["nagios_host"]) ? $thold_item_data["nagios_host"] : ""
+                ),
+
+                "nagios_service" => array(
+                        "friendly_name" => "Nagios Service Name",
+                        "method" => "textbox",
+                        "max_length" => 255,
+                        "description" => "The name of the Service defined in Nagios",
+                        "value" => isset($thold_item_data["nagios_service"]) ? $thold_item_data["nagios_service"] : ""
+                ),
 		
 	);
 
@@ -584,4 +605,4 @@
 	print "</form>\n";
 }
 
-?>
+?>
