nagios mysql 并发_nagios 监控tcp连接数 物理内存 cpu使用率 web并发数 mysql主从状态...

1、centos6.2 操作系统 监控本机tcp连接数 物理内存 cpu使用率 web并发数

查看本机tcp连接状态

netstat -n |awk '/^tcp/ {++s[$NF]} END {for (a in s) print a,s[a]}'

LISTEN:侦听来自远方的TCP端口的连接请求

SYN-SENT:再发送连接请求后等待匹配的连接请求

SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认

ESTABLISHED:代表一个打开的连接

FIN-WAIT-1:等待远程TCP连接中断请求,或先前的连接中断请求的确认

FIN-WAIT-2:从远程TCP等待连接中断请求

CLOSE-WAIT:等待从本地用户发来的连接中断请求

CLOSING:等待远程TCP对连接中断的确认

LAST-ACK:等待原来的发向远程TCP的连接中断请求的确认

TIME-WAIT:等待足够的时间以确保远程TCP接收到连接中断请求的确认

CLOSED:没有任何连接状态

在nagios的插件库里面写下脚本

写脚本ip_cons

#!/bin/bash

#tcp connet

if [ $# != 2 ];then

echo "Usage:$0 -w num1 -c num2"

exit

fi

ip_conns=`netstat -n |grep ESTABLISHED|wc -l`

if [ $ip_conns -lt $1 ];then

echo "OK -connet counts is $ip_conns"

exit 0

fi

if [ $ip_conns -ge $1 -a  $ip_conns -lt $2 ];then

echo "Warning -connet counts is $ip_conns"

exit 1

fi

if [ $ip_conns -ge $2 ];then

echo "Critical -connet counts is $ip_conns"

exit 2

fi

修改配置文件commands.cfg

commands.cfg添加命令

define command{

command_name ip_cons

command_line /usr/local/nagios/libexec/ip_cons 2 5 (2 是警告值 5是临界值可自定义这里只是做实验)

}

之后修改本机的配置文件localhost.cfg

define service{

use                             generic-service

host_name                       localhost

service_description             ip_cons

check_command                   ip_cons

notifications_enabled           1

}

或者是写一个services.cfg文件里里面

define service{

host_name  localhost

service_description ip_cons

check_command ip_cons

max_check_attempts 5

normal_check_interval 3

retry_check_interval 2

check_period 24x7

notification_interval 10

notification_period 24x7

notification_options w,u,c,r

contact_groups admins

}

写入services.cfg文件之后需要在nagios.cfg里面添加

cfg_file=/etc/nagios/objects/services.cfg

之后查看配置文件是否出错 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

没有出错的话  就重新启动nagios服务 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

2、centos6.2 操作系统 监控其他主机tcp连接数 物理内存 cpu使用率 web并发数

写监控脚本

#!/bin/bash

if [ $# != 2 ];then

echo "Usage:$0 -w num1 -c num2"

exit

fi

ip_conns=`netstat -n |grep ESTABLISHED|wc -l`

if [ $ip_conns -lt $1 ];then

echo "OK -connet counts is $ip_conns"

exit 0

fi

if [ $ip_conns -ge $1 -a  $ip_conns -lt $2 ];then

echo "Warning -connet counts is $ip_conns"

exit 1

fi

if [ $ip_conns -ge $2 ];then

echo "Critical -connet counts is $ip_conns"

exit 2

fi

在nrpe.cfg 里面填写监控命令

command[ip_cons]=/usr/local/nagios/libexec/ip_cons 5 9

之后在监控机器上的配置文件里面配置

define service{

use                             generic-service

host_name                       192.168.122.3

service_description             ip_cons

check_command                   check_nrpe!ip_cons

notifications_enabled           1

}

或者是写入services.cfg文件

define service{

host_name  192.168.122.3

service_description ip_cons

check_command check_nrpe!ip_cons

max_check_attempts 5

normal_check_interval 3

retry_check_interval 2

check_period 24x7

notification_interval 10

notification_period 24x7

notification_options w,u,c,r

contact_groups admins

}

之后重新启动nagios服务即可

内存监控的做法基本上和以上做法相同,想来要做这个监控的都是运维的同行,应该可以理解下面就只留下内存的监控脚本

#!/bin/bash

#memory

if [ $# != 2 ];then

echo "Usage:$0 -w num1 -c num2"

exit

fi

total_mem=`free -m |grep Mem|awk '{print $2}'`

free_mem=`free -m |grep Mem|awk '{print $4}'`

used_mem=`free -m |grep Mem|awk '{print $3}'`

if [ $free_mem -gt $1 ];then

echo "OK - total memory  $total_mem MB used  $used_mem MB free $free_mem MB "

exit 0

fi

if [ $free_mem -ge $2 -a $free_mem -le $1 ];then

echo "Warning - total memory  $total_mem MB used $used_mem MB free $free_mem MB"

exit 1

fi

if [ $free_mem -lt $2 ];then

echo "Critical - total memory  $total_mem MB  used $used_mem MB free $free_mem MB"

exit 2

fi

cpu使用率的监控跟上面tcp监控方法类似 留下监控脚本一份

#!/bin/bash

if [ $# != 2 ];then

echo "Usage:$0 -w num1 -c num2"

exit

fi

used_cpu=`vmstat |sed -n 3p|awk -F " " '{print $13}'`

if [ $used_cpu -lt $1 ];then

echo "OK -used_cpu is $used_cpu %"

exit 0

fi

if [ $used_cpu -ge $1 -a  $used_cpu -lt $2 ];then

echo "Warning - used_cpu is $used_cpu %"

exit 1

fi

if [ $used_cpu -ge $2 ];then

echo "Critical - used_cpu is $used_cpu %"

exit 2

fi

此shell脚本中这条命令used_cpu=`vmstat |sed -n 3p|awk -F " " '{print $13}'`是监控系统使用的cpu输出默认是整数 要是想监控剩余cpu的话将以上shell脚本的此条命令修改成used_cpu=`vmstat |sed -n 3p|awk -F " " '{print $15}'`

web并发数 如果是apache做的站点的话 倒是可以直接监控httpd的进程数就可以获得并发数 nginx的话就不行了 可以使用一下脚本

web并发数

#!/bin/bash

#if [ $# != 2 ];then

#echo "Usage:$0 -w num1 -c num2"

#exit

#fi

#w=2

#c=5

ip_conns=`netstat -n |grep '^tcp'|grep ESTABLISHED|grep 80|wc -l`

if [ $ip_conns -lt $1 ];then

echo "OK -connet counts is $ip_conns"

exit 0

fi

if [ $ip_conns -ge $1 -a  $ip_conns -lt $2 ];then

echo "Warning -connet counts is $ip_conns"

exit 1

fi

if [ $ip_conns -ge $2 ];then

echo "Critical -connet counts is $ip_conns"

exit 2

fi

监控mysql主从状态

#!/bin/bash

io=`/usr/local/mysql/bin/mysql -u root -pXXXX -e "show slave status\G" |grep Slave_IO_Running|awk '{print $2}'`

sql=`/usr/local/mysql/bin/mysql -u root -pXXXX -e "show slave status\G" |grep Slave_SQL_Running|awk '{print $2}'`

if [[ $io = Yes && $sql = Yes ]]; then

echo "OK - mysql-replication is running"

exit 0

fi

if [[ $io = No || $sql = No ]];then

echo "Critical - mysql-replication is not working, I/O or SQL is not working properly "

exit 2

fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值