File: 1.15.0a/server/extras/events/detailed_alerts.php (View as Code)

1: 2: /* ------------------------------------------------------------- 3: This file is part of FreeNATS 4: 5: FreeNATS is (C) Copyright 2008-2009 PurplePixie Systems 6: 7: FreeNATS is free software: you can redistribute it and/or modify 8: it under the terms of the GNU General Public License as published by 9: the Free Software Foundation, either version 3 of the License, or 10: (at your option) any later version. 11: 12: FreeNATS is distributed in the hope that it will be useful, 13: but WITHOUT ANY WARRANTY; without even the implied warranty of 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15: GNU General Public License for more details. 16: 17: You should have received a copy of the GNU General Public License 18: along with FreeNATS. If not, see www.gnu.org/licenses 19: 20: For more information see www.purplepixie.org/freenats 21: -------------------------------------------------------------- */ 22: 23: /* Description: 24: 25: This is a custom event handler for FreeNATS v1 and relies on the following 26: event types: alert_open, alert_close, alert_action 27: 28: It also relies on the following FreeNATS class methods: 29: TFreeNATS::ActionAddData, GetNodeTests, GetTest, Event, AddEventHandler 30: TNATS_DB::Query, Fetch_Array, Free 31: 32: USAGE INSTRUCTIONS: 33: 34: Place into the server/base/site/events directory being sure to keep a .php 35: extension on the end of the file. Enable the system variable site.include.events 36: (set to 1) to enable inclusion. 37: 38: To use - first create two alert actions in FreeNATS, one of type message queue 39: with the name "_detailtrigger" and one of whichever type (email or URL) you 40: require for delivery with the name "_detailaction" (note you can configure these 41: names below and also use pre-existing queues of the right type. 42: 43: Now simply point the nodes you wish to gain detail from to use the _detailtrigger 44: alert action. Detail trigger will pad the message with the additional data 45: required and then deliver it via the _detailaction alert action. 46: 47: */ 48: 49: 50: 51: 52: global $NATS; 53: if (isset($NATS)) 54: { 55: 56: 57: function detail_alert_handler($data) 58: { 59: global $NATS; 60: 61: // CONFIGURATION 62: $detail_alert_config=array( 63: "trigger" => "_detailtrigger", 64: "alertaction" => "_detailaction", 65: 66: "status_on_open" => true, // on an alert open include node status 67: "status_on_close" => true, // same for close 68: 69: "summary_on_open" => true, // on open show summary of other alerts 70: "summary_on_close" => true ); // same for close 71: // END OF CONFIGURATION 72: 73: 74: if ($data['event']=="alert_action") 75: { 76: if ($data['name']==$detail_alert_config["trigger"]) return true; // clear 77: else return false; // propgate 78: } 79: else if ($data['event']=="alert_open") 80: { 81: $open=true; 82: $close=false; 83: } 84: else if($data['event']=="alert_close") 85: { 86: $open=false; 87: $close=true; 88: } 89: else return false; 90: 91: // Does this node have the alert action of "trigger" 92: 93: // First what is the trigger aaid 94: $q="SELECT aaid FROM fnalertaction WHERE aname=\"".ss($detail_alert_config["trigger"])."\" LIMIT 0,1"; 95: //echo $q."\n"; 96: $r=$NATS->DB->Query($q); 97: if (!$aa=$NATS->DB->Fetch_Array($r)) 98: { 99: $NATS->Event("No trigger action ".$detail_alert_config["trigger"],10,"Extras","Detail Alert"); 100: return false; // no such trigger alert action 101: } 102: $NATS->DB->Free($r); 103: 104: $aaid=$aa['aaid']; 105: 106: // Second does the node have this alert action 107: $q="SELECT nalid FROM fnnalink WHERE aaid=".$aaid." AND nodeid=\"".ss($data['nodeid'])."\" LIMIT 0,1"; 108: //echo $q."\n"; 109: $r=$NATS->DB->Query($q); 110: if (!$link=$NATS->DB->Fetch_Array($r)) 111: { 112: $NATS->Event("Node does not have trigger action",10,"Extras","Detail Alert"); 113: return false; // no it does not 114: } 115: $NATS->DB->Free($r); 116: 117: 118: $msg=$data['nodeid']." : Alert "; 119: if ($open) $msg.="Opened"; 120: else $msg.="Closed"; 121: $msg.="\n\n"; 122: 123: if ( ( $open && $detail_alert_config['status_on_open'] ) || 124: ( $close && $detail_alert_config['status_on_close'] ) ) 125: { // include node status 126: 127: $tests=$NATS->GetNodeTests($data['nodeid']); 128: foreach($tests as $testid) 129: { 130: $test=$NATS->GetTest($testid); 131: if ($test!==false) // specific comparitor 132: { 133: $msg.=$test['name'].": ".$test['alerttext']." (".$test['lastrunago']." ago)\n"; 134: } 135: } 136: 137: $msg.="\n\n"; 138: } 139: 140: if ( ( $open && $detail_alert_config['summary_on_open'] ) || 141: ( $close && $detail_alert_config['summary_on_close'] ) ) 142: { // include alert summary 143: 144: $msg.="Current Alerts: "; 145: $alerts=$NATS->GetAlerts(); 146: if ( ($alerts===false) || (count($alerts)<=0) ) $msg.="No Alerts"; 147: else 148: { 149: $first=true; 150: foreach($alerts as $alert) 151: { 152: if ($first) $first=false; 153: else $msg.=", "; 154: $msg.=$alert['nodeid']; 155: } 156: } 157: 158: $msg.="\n\n"; 159: } 160: 161: // ok got this far so find output aaid and pass data 162: $q="SELECT aaid FROM fnalertaction WHERE aname=\"".ss($detail_alert_config["alertaction"])."\" LIMIT 0,1"; 163: $r=$NATS->DB->Query($q); 164: if ($row=$NATS->DB->Fetch_Array($r)) 165: { 166: $NATS->DB->Free($r); 167: $NATS->ActionAddData($row['aaid'],$msg); 168: } 169: else 170: { 171: $NATS->Event("Unable to find action ".$detail_alert_config["alertaction"],10,"Extras","Detail Alert"); 172: return false; // failed to find 173: } 174: 175: } 176: 177: $NATS->AddEventHandler("alert_open","detail_alert_handler"); 178: $NATS->AddEventHandler("alert_close","detail_alert_handler"); 179: $NATS->AddEventHandler("alert_action","detail_alert_handler"); // to clear the trigger 180: 181: 182: } // end of NATS block 183: 184: ?>