CACTI monitors the added host state through the SNMP protocol every interval , and in the Cacti database, Host The table records information about the switch, such as status, the most recent downtime (status_fail_date), and the most recent return to normal time ( status_rec_date). To implement fetion monitoring switch status, the switch sends text messages to the specified phone number, and the switch information for the outage cannot be repeated. Idea: Determine the status of the switch (the outage only sends a message once), whether to send text messages. Add the current state of the switch (status_now) and the default switch state (Status_default) in the host table two columns, with a default value of 1 for Normal, are used to compare with the last switch state so that no recurring messages are repeated. The code is as follows: ALTER TABLE ' Host ' ADD COLUMN ' Status_now ' char (2) Not NULL DEFAULT ' 1 ' after ' availability '; ALTER TABLE ' Host ' ADD COLUMN ' Status_default ' char (2) Not NULL default ' 1 ' after ' Status_now '; 1. Recent outage time ; Last recovery time- ; switch down- ; Change record status=0; at this time status columns and Status_default The column values are 0 , 1 - ; Send SMS- ; Change Record status_default=0; at this time status columns and Status_default The column values are 0 , 0 - ; detect switch downtime again, do not relapse SMS ; 2. Recent outage time <= Last recovery time-> Switch back to normal-> change record Status=1; at this Status columns, and Status_default the values for the columns are 1 , 0 - > Send SMS- > Change Record status_default=1; at this Status columns, and Status_default the values for the columns are 1 , 1 - > The switch has not sent a text message. From the above you can see that the switch has undergone four state changes :
status_now status_default |
|
1 |
1 |
Normal, no SMS notification |
0 |
1 |
downtime, SMS notifications |
0 |
0 |
detect downtime again, no SMS notifications |
1 |
0 |
return to normal, SMS notification |
We just need to determine four states and then remove the switch description from the host table ( description ) to a string to submit to Fetion API can.
- Include_once ' conn.php ';
- $sql = "Select Id,hostname,status_fail_date,status_rec_date from ' Cacti". ' Host '; ";
- $query =mysql_query ($sql) or Die (Mysql_error ());
- $nums =mysql_num_rows ($query);
- if ($nums!=0) {
- while ($rs =mysql_fetch_array ($query)) {
- if (Strtotime ($rs [' status_fail_date ']) >strtotime ($rs [' status_rec_date ']) {
- $sql 1= "Update ' cacti '. ' Host ' set ' status_now ' = ' 0 ' where ' host '. ' id ' = '. $rs [' id '];
- $query 1=mysql_query ($sql 1);//Determine switching status down change database Ststus_now value to 0
- }
- if (Strtotime ($rs [' status_fail_date ']) <=strtotime ($rs [' status_rec_date ']) {
- $sql 2= "Update ' cacti '. ' Host ' set ' status_now ' = ' 1 ' where ' host '. ' id ' = '. $rs [' id '];
- $query 2=mysql_query ($sql 2);//Determine Exchange status Normal change database Ststus_default value is 1
- }
- }
- }
- ?>
Copy Code
- Include_once "status.php";
- $sql = "Select Description,status_fail_date,status_rec_date,status_now,status_default from ' Cacti". ' Host '; ";
- $query =mysql_query ($sql) or Die (Mysql_error ());
- $nums =mysql_num_rows ($query);
- if ($nums!=0) {
- while ($rs =mysql_fetch_array ($query)) {
- if ($rs [' status_fail_date ']> $rs [' status_rec_date ']) {
- $sql 1= "Update ' cacti '. ' Host ' set ' status_now ' = ' 0 ' where ' host '. ' id ' = '. $rs [' id '];
- $query 1=mysql_query ($sql 1);
- }
- else if ($rs [' status_fail_date ']<= $rs [' status_rec_date ']) {
- $sql 2= "Update ' cacti '. ' Host ' set ' status_now ' = ' 1 ' where ' host '. ' id ' = '. $rs [' id '];
- $query 2=mysql_query ($sql 2);
- }
- Switch status is not normal, send SMS
- if ($rs [' Status_now ' ==0]) && ($rs [' Status_default ']==1)} {
- $msg = $rs [' description ']. ":d own;"; /SMS Content
- $sql 3= "Update ' cacti '. ' Host ' set ' status_default ' = ' 0 ' where ' host '. ' id ' = '. $rs [' id '];
- $query 3=mysql_query ($sql 3);
- }
- Re-detect switch status is not normal or switch is back to normal, do not send SMS
- else if (($rs [' Status_now ']==1) & ($rs [' Status_default ']==1) | | ($rs [' Status_now ']==0) && ($rs [' Status_default ']==0)}} {
- $msg = ';} SMS content is empty
- Switch-like restore normal, send SMS
- else if ($rs [' Status_now ']==1) && ($rs [' Status_default ']==0)} {
- $msg = $rs [' description ']. ": Recover up;"; /SMS Content
- $sql 4= "Update ' cacti '. ' Host ' set ' status_default ' = ' 1 ' where ' host '. ' id ' = '. $rs [' id '];
- $query 4=mysql_query ($sql 4);
- }
- $info = ($info. $msg);//merge switch status as a text message
- }
- $msg = $info;
- Calling the Fetion interface
- if (!empty ($msg)) {
- $username = 18756064346;//Sender Phone number
- $password = *********;//Sender fetion Password
- $sendto = 18756064346;//fetion receiver phone number
- $curlPost = ' phone= '. UrlEncode ($username). ' &pwd= '. UrlEncode ($password). ' &to= '. UrlEncode ($sendto). ' &msg= '. $msg. ' &type=0 ';
- Echo $curlPost;
- $ch = Curl_init ();//Initialize Curl
- curl_setopt ($ch, Curlopt_url, ' http://3.ibtf.sinaapp.com/f.php ');//crawl specified Web page
- curl_setopt ($ch, Curlopt_header, 0);//Set HEADER
- curl_setopt ($ch, Curlopt_returntransfer, 1);//requires the result to be a string and output to the screen
- curl_setopt ($ch, Curlopt_post, 1);//post Submission method
- curl_setopt ($ch, Curlopt_postfields, $curlPost);
- $data = curl_exec ($ch);//Run Curl
- Curl_close ($ch);
- }else{
- echo "Normal";
- }
- }
- ?>
Copy Code |