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.
- 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'])){
- $sql1="update `cacti`.`host` set `status_now`='0' where `host`.`id`=".$ rs['id'];
- $query1=mysql_query($sql1);//Judge the exchange status and change the database ststus_now value to 0
- }
- if(strtotime($rs['status_fail_date'])<=strtotime( $rs['status_rec_date'])){
- $sql2="update `cacti`.`host` set `status_now`='1' where `host`.`id`=".$rs['id'];
- $query2=mysql_query($sql2);//Judge the exchange status and change the database ststus_default value to 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']){
- $sql1="update `cacti`.`host` set `status_now`='0' where `host`.`id`=".$rs['id '];
- $query1=mysql_query($sql1);
- }
- else if($rs['status_fail_date']<=$rs['status_rec_date']){
- $sql2="update `cacti`.`host ` set `status_now`='1' where `host`.`id`=".$rs['id'];
- $query2=mysql_query($sql2);
- }
- //Switch status is abnormal, send SMS
- if(($rs['status_now'==0])&&($rs['status_default']==1)){
- $msg=$rs['description'].":down;";// SMS content
- $sql3="update `cacti`.`host` set `status_default`='0' where `host`.`id`=".$rs['id'];
- $query3=mysql_query($sql3 );
- }
- //Check again that the switch status is abnormal or the switch has returned to normal, and no SMS will be sent
- else if(($rs['status_now']==1)&($rs['status_default']==1 )||($rs['status_now']==0)&&($rs['status_default']==0)){
- $msg='';}//The text message content is empty
- //Switch-like recovery Normal, send SMS
- else if(($rs['status_now']==1)&&($rs['status_default']==0)){
- $msg=$rs['description'].":recover up;";//SMS content
- $sql4="update `cacti`.`host` set `status_default`='1' where `host`.`id`=".$rs['id'];
- $ query4=mysql_query($sql4);
- }
- $info=($info.$msg);//Merge the switch status into a text message
- }
- $msg=$info;
- //Call the Fetion interface
- if(!empty ($msg)){
- $username = 18756064346;//Sender’s mobile phone number
- $password = *********;//Sender’s Fetion password
- $sendto = 18756064346;//Fetion recipient’s mobile phone No.
- $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');//Catch Specify the web page
- curl_setopt($ch, CURLOPT_HEADER, 0);//Set header
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Require the result to be a string and output it 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
|