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.
- 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 _ right' = '0' where 'host '. 'id' = ". $ rs ['id'];
- $ Query1 = mysql_query ($ sql1); // determines whether the switching status is down and changes the database ststus_now value to 0
- }
- If (strtotime ($ rs ['status _ fail_date ']) <= strtotime ($ rs ['status _ rec_date']) {
- $ Sql2 = "update 'cacti '. 'host' set 'status _ right' = '1' where 'host '. 'id' = ". $ rs ['id'];
- $ Query2 = mysql_query ($ sql2); // check whether the switching status is normal. change the database ststus_default value to 1.
- }
- }
- }
- ?>
- 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 _ right' = '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 _ right' = '1' where 'host '. 'id' = ". $ rs ['id'];
- $ Query2 = mysql_query ($ sql2 );
- }
- // If the switch status is abnormal, send an SMS
- If ($ rs ['status _ right' = 0]) & ($ rs ['status _ default'] = 1 )){
- $ Msg = $ rs ['description']. ": down;"; // text message content
- $ Sql3 = "update 'cacti '. 'host' set 'status _ default' = '0' where 'host '. 'id' = ". $ rs ['id'];
- $ Query3 = mysql_query ($ sql3 );
- }
- // Check whether the switch status is abnormal again or the switch has been restored to normal and no text message is 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 blank.
- // Send an SMS when the switch returns to normal
- Else if ($ rs ['status _ now '] = 1) & ($ rs ['status _ default'] = 0 )){
- $ Msg = $ rs ['description']. ": recover up;"; // text message content
- $ Sql4 = "update 'cacti '. 'host' set 'status _ default' = '1' where 'host '. 'id' = ". $ rs ['id'];
- $ Query4 = mysql_query ($ sql4 );
- }
- $ Info = ($ info. $ msg); // The status of the merged switch is an SMS message.
- }
- $ Msg = $ info;
- // Call the Apsara stack interface
- If (! Empty ($ msg )){
- $ Username = 18756064346; // sender's mobile phone number
- $ Password = *********; // The sender's Feixin password
- $ Sendto = 18756064346; // The mobile phone number of the recipient
- $ 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'); // capture the specified webpage
- Curl_setopt ($ ch, CURLOPT_HEADER, 0); // Set the header
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is 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 ";
- }
- }
- ?>
|