search
HomeBackend DevelopmentPHP TutorialCall Fetion interface to implement cacti monitoring notification

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'])$sql2="update `cacti`.`host` set `status_now`='1' where `host`.`id`=".$rs['id'];
  12. $query2=mysql_query($sql2);//Judge the exchange status and change the database ststus_default value to 1
  13. }
  14. }
  15. }
  16. ?>
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']$sql2="update `cacti`.`host ` set `status_now`='1' where `host`.`id`=".$rs['id'];
  12. $query2=mysql_query($sql2);
  13. }
  14. //Switch status is abnormal, send SMS
  15. if(($rs['status_now'==0])&&($rs['status_default']==1)){
  16. $msg=$rs['description'].":down;";// SMS content
  17. $sql3="update `cacti`.`host` set `status_default`='0' where `host`.`id`=".$rs['id'];
  18. $query3=mysql_query($sql3 );
  19. }
  20. //Check again that the switch status is abnormal or the switch has returned to normal, and no SMS will be sent
  21. else if(($rs['status_now']==1)&($rs['status_default']==1 )||($rs['status_now']==0)&&($rs['status_default']==0)){
  22. $msg='';}//The text message content is empty
  23. //Switch-like recovery Normal, send SMS
  24. else if(($rs['status_now']==1)&&($rs['status_default']==0)){
  25. $msg=$rs['description'].":recover up;";//SMS content
  26. $sql4="update `cacti`.`host` set `status_default`='1' where `host`.`id`=".$rs['id'];
  27. $ query4=mysql_query($sql4);
  28. }
  29. $info=($info.$msg);//Merge the switch status into a text message
  30. }
  31. $msg=$info;
  32. //Call the Fetion interface
  33. if(!empty ($msg)){
  34. $username = 18756064346;//Sender’s mobile phone number
  35. $password = *********;//Sender’s Fetion password
  36. $sendto = 18756064346;//Fetion recipient’s mobile phone No.
  37. $curlPost = 'phone='.urlencode($username).'&pwd='.urlencode($password).'&to='.urlencode($sendto).'&msg='.$msg.'&type=0 ';
  38. echo $curlPost;
  39. $ch = curl_init();//Initialize curl
  40. curl_setopt($ch,CURLOPT_URL,'http://3.ibtf.sinaapp.com/f.php');//Catch Specify the web page
  41. curl_setopt($ch, CURLOPT_HEADER, 0);//Set header
  42. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Require the result to be a string and output it to the screen
  43. curl_setopt($ch, CURLOPT_POST, 1) ;//Post submission method
  44. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  45. $data = curl_exec($ch);//Run curl
  46. curl_close($ch);
  47. }else{
  48. echo "normal";
  49. }
  50. }
  51. ?>
Copy code


Statement
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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!