Home > Backend Development > PHP Tutorial > Call Fetion interface to implement cacti monitoring notification

Call Fetion interface to implement cacti monitoring notification

WBOY
Release: 2016-07-25 09:01:53
Original
1018 people have browsed it

Cacti monitors the status of the added host through the snmp protocol at intervals. In the Cacti database, the host table records the relevant information of the switch, such as status (status ), the most recent downtime time (status_fail_date), the most recent recovery time (status_rec_date). To enable Fetion to monitor the status of the switch, when the switch down, a text message will be sent to the designated mobile phone number, and information about the downed switch cannot be sent repeatedly. Idea: Determine the status of the switch (only sends information once when it is down) and whether to send text messages. Add the current status of the switch (status_now) and the default switch status (status_default) to the host table. Their default values ​​are both 1, which means normal, and is used with the above One switch status comparison to avoid repeated text messaging.

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 downtime >Latest recovery time—>Switch down—>Change recordstatus=0;At this timestatus column and The values ​​of the status_default column are 0, 1>Send SMS—>Change recordstatus_default=0;at this time status The values ​​of the column and the status_default column are respectively 0, 0>Detect the switch is down again and do not send repeated text messages;

2. The most recent downtime timeThe most recent recovery time—>The switch is back to normal—>Change recordstatus=1;at this timestatus column and The values ​​of the status_default column are 1, 0>Send SMS—>Change recordstatus_default=1; At this timestatus The values ​​of the column and the status_default column are respectively 1, 1>The switch has not sent SMS.

It can be seen from the above that the switch has experienced four status changes:

status_now

status_default

Result

1

1

Normal, no SMS notification

0

1

Downtime, SMS notification

0

0

Detected downtime again, no SMS notification

1

0

Return to normal, SMS notification

We only need to determine the four states and then take out the switch description (description) from the host table, combine it into a string and submit it to Fetionapi.

  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_now`='0' where `host`.`id`=".$ rs['id'];
  9. $query1=mysql_query($sql1);//Judge the exchange status and change 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_now`='1' where `host`.`id`=".$rs['id'];
  13. $query2=mysql_query($sql2);//Judge the exchange status and change the database ststus_default value to 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. $sql1="update `cacti`.`host` set `status_now`='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_now`='1' where `host`.`id`=".$rs['id'];
  13. $query2=mysql_query($sql2);
  14. }
  15. //Switch status is abnormal, send SMS
  16. if(($rs['status_now'==0])&&($rs['status_default']==1)){
  17. $msg=$rs['description'].":down;";// SMS content
  18. $sql3="update `cacti`.`host` set `status_default`='0' where `host`.`id`=".$rs['id'];
  19. $query3=mysql_query($sql3 );
  20. }
  21. //Check again that the switch status is abnormal or the switch has returned to normal, and no SMS will be 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 empty
  24. //Switch-like recovery Normal, send SMS
  25. else if(($rs['status_now']==1)&&($rs['status_default']==0)){
  26. $msg=$rs['description'].":recover up;";//SMS 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);//Merge the switch status into a text message
  31. }
  32. $msg=$info;
  33. //Call the Fetion interface
  34. if(!empty ($msg)){
  35. $username = 18756064346;//Sender’s mobile phone number
  36. $password = *********;//Sender’s Fetion password
  37. $sendto = 18756064346;//Fetion recipient’s mobile phone No.
  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');//Catch Specify the web page
  42. curl_setopt($ch, CURLOPT_HEADER, 0);//Set header
  43. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Require the result to be a string and output it 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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template