The Nagios system provides a plugin Nrpe. Nagios runs it periodically to obtain various state information for the remote server. The relationship between them is as follows:
Nagios uses Nrpe to manage services remotely
1. Nagios executes the Check_nrpe plugin installed inside it and tells Check_nrpe to detect which services.
2. Connect the Nrpe on the remote machine via the Ssl,check_nrpe daemon
3. NRPE runs various local plugins to detect local service and status (Check_disk,.. etc
4. Finally, NRPE passes the results of the test to the host side Check_nrpe,check_nrpe and sends the results to the Nagios status queue.
5. Nagios reads the information in the queue, then displays the results.
In fact, we installed Nagios is just a framework, as to what kind of function, need to write a plugin to complete the supplement, the specific language can choose C++,shell,python, etc., Here I am using Python2.7.3, the system is UBUNTU14.04LTS,NAGIOS3 version is 3.5.1
Because the original Nagios did not check the memory usage of the system, I wrote a script check_mem.py to check the system memory in Python, as needed, as follows
#!/usr/bin/env pythonimport reimport subprocessimport osimport Sysmem_warning=0.80mem_critical=0.90#nagios has four monitoring states, 0 Green stands for ok,1 yellow for warning,2 red on behalf of critical,3 Brown for Unknownstate_ok=0state_warning=1state_critical=2state_unknown=3try: #此处使用subprocess. Popen always error, so swapped with Os.popen to execute the shell, I also reported this error to the official, but did not get a reply, I hope that Daniel can help the doubts #mem_info =subprocess. Popen (' Free ', shell=true,stdout=subprocess. PIPE, # stderr= (open ('/tmp/check_mem_errors.txt ', ' W ')) #mem_info. Stdout.readline () #inf O=mem_info.stdout.readline () mem_info=os.popen (' Free ') mem_info.readline () Info=mem_info.readline () Info=info. Strip (' \ n ') info_list=re.split (' \s+ ', info) mem_total=long (info_list[1]) Mem_free1=long (info_list[3]) #info =mem _info.stdout.readline () Info=mem_info.readline () Info=info.strip (' \ n ') info_list=re.split (' \s+ ', info) mem_free 2=long (info_list[2]) #in Linux, free memory is freed+cached memory Mem_free=mem_free1+mem_free2 MEM_USED_PERCEntage=float ("%.2f"% (float (mem_free)/mem_total)) if Mem_used_percentage < Mem_warning:print "Ok:total Mem Ory%ld kb, free memory%ld KB, used percentage%.2f "% (mem_total,mem_free,mem_used_percentage) sys. Exit (0) Elif Mem_used_percentage < Mem_critical:print "Warning:total memory%ld KB, free memory%ld KB, use d percentage%.2f "% (mem_total,mem_free,mem_used_percentage) sys.exit (state_warning) Else: Print "Critical:total Memory%ld KB, free memory%ld KB, used percentage%.2f"% (Mem_total,mem_free,mem_u Sed_percentage) Sys.exit (state_critical) except Valueerror:print "UNKNOWN" Sys.exit (State_unknown)
The results are as follows:
The specific configuration is as follows:
1. Put this script in the/usr/lib/nagios/plugins directory
2. Add in/etc/nagios3/commands.cfg:
Define command{ command_name check_meminfo command_line $USER 1$/check_mem.py
3. Then add in/etc/nagios3/conf.d/services_nagios2.cfg:
Define service{ host_name localhost service_description memorydetails check_command check_meminfo Use Generic-service notification_interval 0}
4, in the monitoring machine, login 127.0.0.1/nagios3/, you can see the results
Reference article: http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Nagios3 Add a plugin written by Python