Call the Apsara stack interface to implement cacti monitoring notifications

Source: Internet
Author: User
Call the Apsara stack interface to implement cacti monitoring notifications

Cacti uses the snmp protocol to monitor the status of added hosts at intervals. in the Cacti database, the host table records information about switches, such as status ), recent downtime (status_fail_date) and recent recovery time (status_rec_date ). To monitor the switch status of Apsara stack, send a text message to the specified mobile phone number when the switch is down, and the switch information cannot be sent repeatedly. Train of Thought: determine the status of the switch (only one message is sent when the switch is down) and whether to send text messages. In the host table, add the current status (status_now) and default status (status_default) of the switch. the default value is 1, indicating that the switch is normal. this is used to compare the status with the previous switch so that no text messages are sent repeatedly.

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 _ right ';

1. recent downtime> recent recovery time-> switch downtime-> Change Record status = 0; in this case, the values of the status column and the status_default column are 0, 1, and 1 respectively; in this case, the values in the status column and status_default column are respectively 0, 0-> Re-detect switch downtime without repeatedly sending text messages;

2. recent downtime <= recent recovery time-> switch recovery normal-> Change Record status = 1; in this case, the values in the status column and status_default column are respectively 1, 0-> Send SMS-> Change record status_default = 1. in this case, the values in the status column and status_default column are respectively 1, 1-> The Switch already exists, do not send text messages.

From this we can see that the switch has undergone four changes:

Status_now

Status_default

Result

1

1

Normal, no SMS notification

0

1

Downtime, SMS notification

0

0

Detects downtime again without SMS notifications

1

0

Resume normal, SMS notification

We only need to judge the four statuses and then extract the vSwitch description from the host table and combine them into strings and submit them to the Apsara stack api.

  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. $ Sql1 = "update 'cacti '. 'host' set 'status _ right' = '0' where 'host '. 'id' = ". $ rs ['id'];
  9. $ Query1 = mysql_query ($ sql1); // determines whether the switching status is down and changes the database ststus_now value to 0
  10. }
  11. If (strtotime ($ rs ['status _ fail_date ']) <= strtotime ($ rs ['status _ rec_date']) {
  12. $ Sql2 = "update 'cacti '. 'host' set 'status _ right' = '1' where 'host '. 'id' = ". $ rs ['id'];
  13. $ Query2 = mysql_query ($ sql2); // check whether the switching status is normal. change the database ststus_default value to 1.
  14. }
  15. }
  16. }
  17. ?>

  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. $ Sql1 = "update 'cacti '. 'host' set 'status _ right' = '0' where 'host '. 'id' = ". $ rs ['id'];
  9. $ Query1 = mysql_query ($ sql1 );
  10. }
  11. Else if ($ rs ['status _ fail_date '] <= $ rs ['status _ rec_date']) {
  12. $ Sql2 = "update 'cacti '. 'host' set 'status _ right' = '1' where 'host '. 'id' = ". $ rs ['id'];
  13. $ Query2 = mysql_query ($ sql2 );
  14. }
  15. // If the switch status is abnormal, send an SMS
  16. If ($ rs ['status _ right' = 0]) & ($ rs ['status _ default'] = 1 )){
  17. $ Msg = $ rs ['description']. ": down;"; // text message content
  18. $ Sql3 = "update 'cacti '. 'host' set 'status _ default' = '0' where 'host '. 'id' = ". $ rs ['id'];
  19. $ Query3 = mysql_query ($ sql3 );
  20. }
  21. // Check whether the switch status is abnormal again or the switch has been restored to normal and no text message is sent.
  22. Else if ($ rs ['status _ now '] = 1) & ($ rs ['status _ default'] = 1) | ($ rs ['status _ now '] = 0) & ($ rs ['status _ default'] = 0 )){
  23. $ Msg = '';} // The text message content is blank.
  24. // Send an SMS when the switch returns to normal
  25. Else if ($ rs ['status _ now '] = 1) & ($ rs ['status _ default'] = 0 )){
  26. $ Msg = $ rs ['description']. ": recover up;"; // text message content
  27. $ Sql4 = "update 'cacti '. 'host' set 'status _ default' = '1' where 'host '. 'id' = ". $ rs ['id'];
  28. $ Query4 = mysql_query ($ sql4 );
  29. }
  30. $ Info = ($ info. $ msg); // The status of the merged switch is an SMS message.
  31. }
  32. $ Msg = $ info;
  33. // Call the Apsara stack interface
  34. If (! Empty ($ msg )){
  35. $ Username = 18756064346; // sender's mobile phone number
  36. $ Password = *********; // The sender's Feixin password
  37. $ Sendto = 18756064346; // The mobile phone number of the recipient
  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'); // capture the specified webpage
  42. Curl_setopt ($ ch, CURLOPT_HEADER, 0); // Set the header
  43. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is 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. ?>

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.