温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-06 15:20:25 来源:网络 阅读:1350 作者:xiaoyuan234 栏目:移动开发

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI