Nagios Monitor use Pnp4nagios custom template Paint Instance

Source: Internet
Author: User
Tags rrd

Now there are a lot of people using Zabbix, but I think if the number of server monitoring is not much, the old monitoring system Nagios is very good. Nagios Alarm function is very powerful, and the program is small, resource footprint. Nagios default does not support drawing, you can match cacti, but build more complex. Individuals are still accustomed to using Pnp4nagios.

Nagios and Pnp4nagios A key installation script reference to my Github:https://github.com/zhangnq/nagios/tree/master/setup

Pnp4nagios The default diagram is very unattractive, and if there are more than one item in the monitor item, Pnp4nagios will display multiple items separately. Here the blogger uses the monitor memory script as an example to introduce how to use the Pnp4nagios custom template to achieve a beautiful monitoring diagram.

Nagios Client

A script that requires memory monitoring to be added to the client is not provided by the default plugin.

1, add memory monitoring script, content similar to the following:

#!/bin/bash

#nagios Exit code
State_ok=0
State_warning=1
state_critical=2
State_unknown=3

Help () {
Local command= ' basename $ '
echo "NAME
${command}--Check memory status
Synopsis
${command} [OPTION]
DESCRIPTION
-W warning=
-C critical=
USAGE:
$0-w 50%-C 60% "1>&2
Exit ${state_warning}
}

Check_num () {
Local num_str= "$"
echo ${num_str}|grep-e ' ^[0-9]+$ ' >/dev/null 2>&1 | | Local stat= ' not a positive integers! '
if ["${stat}" = ' not a positive integers! ']; Then
echo "${num_str} ${stat}" 1>&2
Exit ${state_warning}
Else
Local num_int= ' echo ${NUM_STR}*1|BC '
If [${num_int}-lt 0];then
echo "${num_int} must be greater than 0!" 1>&2
Exit ${state_warning}
Fi
Fi
}

#input
While getopts W:c: opt
Todo
Case "$opt" in
W
warning= $OPTARG
Warning_num= ' echo ' ${warning} ' |sed ' s/%//g '
Check_num "${warning_num}"
;;
C
Critical= $OPTARG
Critical_num= ' echo ' ${critical} ' |sed ' s/%//g '
Check_num "${critical_num}"
;;
*) help;;
Esac
Done
Shift $[$OPTIND-1]

[$#-gt 0-o-Z "${warning_num}"-o-z "${critical_num}"] && Help

If [-N "${warning_num}"-a-n "${critical_num}"];then
If [${warning_num}-ge ${critical_num}];then
echo "-W ${warning} must lower Than-c ${critical}!" 1>&2
Exit ${state_unknown}
Fi
Fi

Datas= ' awk-f ': |k ' $2~/[0-9]+/{datas[$1]=$2}end{for (data in datas) {print data ' = ' datas[data]} '/proc/meminfo | Grep-ev ' [) | (]'`

Var= ' echo ' ${datas} ' |sed ' s///g '
Eval "${var}"

Memused= ' echo ${MEMTOTAL}-${MEMFREE}-${CACHED}-${BUFFERS}|BC '
Memusage= ' echo ' ${memused}/${memtotal}*100 ' |bc-l '
Memusage_num= ' echo ${MEMUSAGE}/1|BC '
#echo ${memusage_num}
Memtotal_mb= ' echo ${MEMTOTAL}/1024|BC '
Memused_mb= ' echo ${MEMUSED}/1024|BC '
Memfree_mb= ' echo ${MEMFREE}/1024|BC '
Cached_mb= ' echo ${CACHED}/1024|BC '
Buffers_mb= ' echo ${BUFFERS}/1024|BC '

Message () {
Local stat= "$"
echo "MEMORY is ${stat}-Usage: ${memusage_num}%. Total: ${memtotal_mb} MB Used: ${memused_mb} MB free: ${memfree_mb} MB | USED=${MEMUSED_MB};; CACHED=${CACHED_MB};; BUFFERS=${BUFFERS_MB};; FREE=${MEMFREE_MB};; "
}

[${memusage_num}-lt ${warning_num}] && message "OK" && exit ${STATE_OK}
[${memusage_num}-ge ${critical_num}] && message "Critical" && exit ${state_critical}
[${memusage_num}-ge ${warning_num}] && message "Warning" && exit ${state_warning}
The script path is typically/usr/local/nagios/libexec, named check_mem.sh.

2, and then modify the nrpe.cfg configuration file, restart Nrpe, the command is similar to the following.

wget http://download.chekiang.info/nagios/check_mem.sh
chmod +x check_mem.sh
Chown Nagios:nagios check_mem.sh

Cat >>/usr/local/nagios/etc/nrpe.cfg<< "EOF"
Command[check_mem]=/usr/local/nagios/libexec/check_mem.sh-w 80%-C 90%
Eof
Sleep 3
/root/restart_nrpe.sh
Nagios Service Side

1, after the client adds the Check_mem.sh plug-in, adds the monitoring service Check_mem in the service end, restarts the Nagios.

Define service{
        use                      LOCAL-SERVICE,SRV-PNP
         host_name                www.sijitao.net
        service_description      Check memory usage
        check_command            Check_nrpe!check_mem
         notification_options    w,c
}
2, enter the Check_command profile directory for Pnp4nagios, such as/usr/local/ pnp4nagios/etc/check_commands/. There are several sample files in the default directory, add Check_nrpe.cfg, and the contents are as follows.

Custom_template = 1 #使用命令的第一个参数做自定义模板名
DATATYPE = Gauge #数据类型为即时数值
Use_min_on_create = 0 #绘图数据最小值为0 to exclude negative values caused by some error overflow
3, enter the Pnp4nagios template template directory, such as/usr/local/pnp4nagios/share/templates.dist. Add check_mem.php, memory similar to the following.

$alpha = ' CC ';
$colors = Array (
' #850707 '. $alpha,
' #FFDB87 '. $alpha,
' #25345C '. $alpha,
' #88008A '. $alpha,
' #4F7774 '. $alpha,
);
$opt [1] = sprintf ('-t 55-l 0--vertical-label "Bytes"--title "%s/memory Usage" ", $hostname);
$def [1] = ';
$count = 0;
foreach ($DS as $i) {
$def [1]. = RRD::d EF ("var$i", $rrdfile, $DS [$i], ' AVERAGE ');
if ($i = = ' 1 ') {
$def [1]. = Rrd::area ("var$i", $colors [$count], Rrd::cut (Ucfirst ($NAME [$i]), 15);
} else {
$def [1]. = Rrd::area ("var$i", $colors [$count], Rrd::cut (Ucfirst ($NAME [$i]), ' STACK ');
}
$def [1]. = Rrd::gprint ("var$i", Array (' Last ', ' MAX ', ' AVERAGE '), "%4.2lf%s\\t");
$count + +;
}
Add a few minutes after the completion of the Nagios to generate data to see the Pnp4nagios custom template effect diagram

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.