Use Linux C to develop the Nagios monitoring plug-in series-plug-in development for monitoring MySQL status

Source: Internet
Author: User

Due to work needs, recently I was assigned to the system O & M department of the company for a while, during which I met Nagios, an open source system monitoring software. But before I got into touch with the system O & M work, I really didn't know the name of Nagios or where it was holy. As a senior coders, I didn't even know such an artifact, shame! But shame and shame, the days still need to pass, take money and disaster recovery, since has been sent to the system O & M department, how to find something to do. As a result, writing several monitoring plug-ins allows the leaders to view Real-time Monitoring charts on the monitoring dashboard is also a highlight. The powerful functions of Nagios are not described here. The related installation and configuration are not the focus of this Article. We mainly explain how to write the monitoring plug-in of Nagios.

Nagios does not have any special functions. All monitoring functions come from various plug-ins. Nagios is just a platform and has developed a set of unified standards, plug-ins can collaborate with each other and generate productivity as long as they are developed according to the standard, which is a bit similar to Maven. We developed a MySQL database monitoring plug-in that monitors the current number of MySQL connections and the number of SQL statements executed over a long period of time and generates statistical graphs. First, we will introduce our development environment:

 
Operating System: RedHat Linux as 5.8 64-bit Database: the MySQL server Nagios: 3.5 that comes with the operating system. It uses the passive monitoring mode to display the following figure: pnp4nagios plug-in development language: Linux C compiler: GCC 4.1.2 external function library in Linux: mysqlclient library officially provided by MySQL

The mysqlclient function library can be found on the Linux installation CD. You do not need to download it. The following is the RPM package installed on my machine:

The red box in the figure shows the RPM installation package, which can be found on your Linux installation CD.

Next we will introduce the Nagios data format. The following is an example:

OK: MySQL alive: threads_connected = 1 | 'threads _ connected' = 1; 25; 30;

The Nagios data format is divided into two parts, separated by pipeline characters in the middle. The first half is displayed on the monitoring front page by Nagios, and the second half is used for graphic display. We mainly focus on the second half. We can see three numbers separated by semicolons. 1 indicates the current value, that is, the number of current connections in MySQL, and 25 indicates the warning line in the image, in the image, a yellow line is used to represent the dangerous line, and 30 is used to represent the dangerous line. Let's take a look at the graphic display effect, as shown in (some plug-ins downloaded online do not provide the second half of the data, which is called prefdata. Therefore, xxxx cannot be found when displaying the image. XML file error information, then you should check whether your plug-in supports prefdata output ):

Next, let's take a look at how the first half of the data is displayed on the Nagios front-end page, for example:

The red box contains the first half of the displayed data.

After learning about the data format collected by Nagios, you can write monitoring plug-ins. In fact, our MySQL monitoring plug-in is very simple. First, connect to the specified MySQL server through the function provided by the mysqlclient library, and then execute the "show status like '% XXXX %'" statement, this SQL statement is used to query the current status information of the MySQL server. XXXX is the name of the state attribute variable to be queried. For example, the threads_connected variable name in the preceding example is used to query the current number of connections of the MySQL server. Of course, there are many other state variable names. You can query the MySQL user manual to obtain more information. Finally, output the query results in the data format specified by Nagios. Additional plug-insProgramThree integers are returned to inform Nagios of the final state, which will be described in the subsequent content.

Finally, the presentationCodeBecause our plug-in program is very simple, I only provide the display of header files, and you can download the attachment for viewing the implementation code. The header file is defined as follows:

 
# Ifndef prepare _ # define mysql_health_check_h _ // Nagios return status value # define OK 0 # define warning 1 # define critical 2 # define unknown 3 # ifdef winnt # include <winsock2.h> # endif # include "MySQL. H "/*** define data struct to MySQL Status values */typedef struct {int success; char * variable_name; int variable_value;} mysqlstatus; /*** define data struct to connection MySQL parameters */typedef struct {char * host_name; char * user_name; char * password; char * db_name;} mysqlparameter; /*** print this plugin help message */void usage ();/*** check MySQL status then return Nagios prefdata format string */INT check_mysql_health (const mysqlstatus *, Int, INT);/*** query MySQL current status, return NULL is occur errors */mysqlstatus query_mysql_status (const mysqlparameter, char *); # endif/* mysql_health_check_h _*/

 

I have defined two struct types. mysqlstatus is used to store query results, and mysqlparameter is used to store database connection parameter data (Host IP address, user name, password, and other information ). Four constant macros are also defined to return the check results to Nagios. They are:

OK-0: return if the check status is normal;

Warning-1: returned when the check status is warning;

Critical-2: return if the check status is dangerous;

Unknown-3: returned when the check status is unknown;

Nagios will display different states based on the return values of the plug-in program. If a warning is triggered, it will be yellow and red in case of danger. In addition, three functions are defined. The usage function is used to display the usage of the plug-in when you provide the-H parameter. As a Convention, each plug-in should implement the help method and provide the-h Parameter option to inform you of the call method and parameter meaning of the plug-in. The query_mysql_status function connects to the database based on the input parameters, runs the "show status" Statement, and assigns the query status result to the mysqlstatus struct and returns it. The check_mysql_health function compares the struct returned by the query_mysql_status function with the-W and-C parameters provided by the user. If the value of the-W parameter is greater than that of the-C parameter, this function returns 1 (warning). If it is greater than the value of-C, this function returns 2 (critical); otherwise, 0 (OK ), if an error occurs during the function execution, 3 (unknown) is returned ).

Okay, General Program ImplementationAlgorithmThese are indeed very simple. Finally, we need to write a makefile for compiling and generating executable files in the Linux environment. The contents of the makefile are as follows:

ALL: check_mysql_health # Which compilercc = GCC # options for releasecflags =-O2-wall-C-fmessage-length = 0check_mysql_health: Main. O check_mysql_health.o $ (CC)-L/usr/lib/MySQL-L/usr/lib64/MySQL-O check_mysql_health main. O check_mysql_health.o-Example: check_mysql_health.c check_mysql_health.h $ (CC)-I/usr/include/MySQL $ (cflags) check_mysql_health.cmain.o: Main. c check_mysql_health.h $ (CC)-I/usr/include/MySQL $ (cflags) Main. cclean:-rm-RF main. O check_mysql_health.o check_mysql_health

Upload All files to a Linux directory and execute the make command in the directory. If an error occurs when connecting to the database, check whether the mysqlclient library is installed in the system. If the compilation is complete, the check_mysql_health file is generated in the current directory. Use the CHMOD + x command to add execution properties to the file, and enter the following command for testing:

 
./Check_mysql_health-H

This command calls the usage function and displays the call methods and parameters of the plug-in:

We can see that the plug-in has a total of 8 Parameter options, and the meaning of each parameter can be viewed. The following is an example:

./Check_mysql_health-h xxx. XXX-u root-P password-d test-W 30-C 40

Finally, copy the plug-in to the libexe directory of Nagios to use it like the plug-in provided by Nagios.

 

Conclusion:

Nagios is like a stage where actors of all kinds can make full use of their talents. In addition to C/C ++, Nagios plug-ins can also use shell and Perl. Of course, Java can also be used, but I don't like the plug-ins compiled by Perl, it is not easy to run the Perl plug-in. You must be overwhelmed by installing various dependent Perl modules. Complete plug-ins are attached.Source codeFor your reference and exchange.

Attachment: check_mysql_health.tar.gz

 

 

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.