Call the Fetion interface to implement cacti monitoring notifications

Source: Internet
Author: User
Tags urlencode

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.

    1. Include_once ' conn.php ';
    2. $sql = "Select Id,hostname,status_fail_date,status_rec_date from ' Cacti". ' Host '; ";
    3. $query =mysql_query ($sql) or Die (Mysql_error ());
    4. $nums =mysql_num_rows ($query);
    5. if ($nums!=0) {
    6. while ($rs =mysql_fetch_array ($query)) {
    7. if (Strtotime ($rs [' status_fail_date ']) >strtotime ($rs [' status_rec_date ']) {
    8. $sql 1= "Update ' cacti '. ' Host ' set ' status_now ' = ' 0 ' where ' host '. ' id ' = '. $rs [' id '];
    9. $query 1=mysql_query ($sql 1);//Determine switching status down change database Ststus_now value to 0
    10. }
    11. if (Strtotime ($rs [' status_fail_date ']) <=strtotime ($rs [' status_rec_date ']) {
    12. $sql 2= "Update ' cacti '. ' Host ' set ' status_now ' = ' 1 ' where ' host '. ' id ' = '. $rs [' id '];
    13. $query 2=mysql_query ($sql 2);//Determine Exchange status Normal change database Ststus_default value is 1
    14. }
    15. }
    16. }
    17. ?>
Copy Code
  1. Include_once "status.php";
  2. $sql = "Select Description,status_fail_date,status_rec_date,status_now,status_default from ' Cacti". ' Host '; ";
  3. $query =mysql_query ($sql) or Die (Mysql_error ());
  4. $nums =mysql_num_rows ($query);
  5. if ($nums!=0) {
  6. while ($rs =mysql_fetch_array ($query)) {
  7. if ($rs [' status_fail_date ']> $rs [' status_rec_date ']) {
  8. $sql 1= "Update ' cacti '. ' Host ' set ' status_now ' = ' 0 ' where ' host '. ' id ' = '. $rs [' id '];
  9. $query 1=mysql_query ($sql 1);
  10. }
  11. else if ($rs [' status_fail_date ']<= $rs [' status_rec_date ']) {
  12. $sql 2= "Update ' cacti '. ' Host ' set ' status_now ' = ' 1 ' where ' host '. ' id ' = '. $rs [' id '];
  13. $query 2=mysql_query ($sql 2);
  14. }
  15. Switch status is not normal, send SMS
  16. if ($rs [' Status_now ' ==0]) && ($rs [' Status_default ']==1)} {
  17. $msg = $rs [' description ']. ":d own;"; /SMS Content
  18. $sql 3= "Update ' cacti '. ' Host ' set ' status_default ' = ' 0 ' where ' host '. ' id ' = '. $rs [' id '];
  19. $query 3=mysql_query ($sql 3);
  20. }
  21. Re-detect switch status is not normal or switch is back to normal, do not send SMS
  22. else if (($rs [' Status_now ']==1) & ($rs [' Status_default ']==1) | | ($rs [' Status_now ']==0) && ($rs [' Status_default ']==0)}} {
  23. $msg = ';} SMS content is empty
  24. Switch-like restore normal, send SMS
  25. else if ($rs [' Status_now ']==1) && ($rs [' Status_default ']==0)} {
  26. $msg = $rs [' description ']. ": Recover up;"; /SMS Content
  27. $sql 4= "Update ' cacti '. ' Host ' set ' status_default ' = ' 1 ' where ' host '. ' id ' = '. $rs [' id '];
  28. $query 4=mysql_query ($sql 4);
  29. }
  30. $info = ($info. $msg);//merge switch status as a text message
  31. }
  32. $msg = $info;
  33. Calling the Fetion interface
  34. if (!empty ($msg)) {
  35. $username = 18756064346;//Sender Phone number
  36. $password = *********;//Sender fetion Password
  37. $sendto = 18756064346;//fetion receiver phone number
  38. $curlPost = ' phone= '. UrlEncode ($username). ' &pwd= '. UrlEncode ($password). ' &to= '. UrlEncode ($sendto). ' &msg= '. $msg. ' &type=0 ';
  39. Echo $curlPost;
  40. $ch = Curl_init ();//Initialize Curl
  41. curl_setopt ($ch, Curlopt_url, ' http://3.ibtf.sinaapp.com/f.php ');//crawl specified Web page
  42. curl_setopt ($ch, Curlopt_header, 0);//Set HEADER
  43. curl_setopt ($ch, Curlopt_returntransfer, 1);//requires the result to be a string and output to the screen
  44. curl_setopt ($ch, Curlopt_post, 1);//post Submission method
  45. curl_setopt ($ch, Curlopt_postfields, $curlPost);
  46. $data = curl_exec ($ch);//Run Curl
  47. Curl_close ($ch);
  48. }else{
  49. echo "Normal";
  50. }
  51. }
  52. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.