#!/bin/bash
#IT_BEGIN
#IT_TYPE=3
#IT MIDWARE_NGINX_DISCOVERY|discovery.nginx[disc]
#原型指标
#IT_RULE MIDWARE_Nginx_hostname|hostname[{#port}]
#IT_RULE MIDWARE_Nginx_ip|ip[{#port}]
#IT_RULE MIDWARE_Nginx_port|port[{#port}]
#IT_RULE MIDWARE_Nginx_pid|pid[{#port}]
#IT_RULE MIDWARE_Nginx_version|version[{#port}]
#IT_RULE MIDWARE_Nginx_nginxpath|nginx_path[{#port}]
#IT_RULE MIDWARE_Nginx_workernum|worker_num[{#port}]
#IT_RULE MIDWARE_Nginx_startuser|start_user[{#port}]
#IT_RULE MIDWARE_Nginx_headerbuffer|header_buffer[{#port}]
#IT_RULE MIDWARE_Nginx_largebuffer|large_buffer[{#port}]
#IT_RULE MIDWARE_Nginx_defaultcharset|default_charset[{#port}]
#IT_RULE MIDWARE_Nginx_eventmode|event_mode[{#port}]
#IT_RULE MIDWARE_Nginx_hashsize|hash_size[{#port}]
#IT_RULE MIDWARE_Nginx_nginxmaxsize|nginx_max_size[{#port}]
#IT_RULE MIDWARE_Nginx_errlog|err_log[{#port}]
#IT_RULE MIDWARE_Nginx_defaultfiletype|default_file_type[{#port}]
#IT_RULE MIDWARE_Nginx_keepalivetimeout|keepalive_timeout[{#port}]
#IT_RULE MIDWARE_Nginx_upstreammode|upstream_mode[{#port}]
#IT_RULE MIDWARE_Nginx_serverip|server_ip[{#port}]
#IT_RULE MIDWARE_Nginx_allowip|allow_ip[{#port}]
#IT_RULE MIDWARE_Nginx_denyip|deny_ip[{#port}]
#IT_RULE MIDWARE_Nginx_listenport|listen_port[{#port}]
#IT_RULE MIDWARE_Nginx_rootpath|root_path[{#port}]
#IT_RULE MIDWARE_Nginx_accesslog|access_log[{#port}]
#IT_RULE MIDWARE_Nginx_servername|server_name[{#port}]
#IT_RULE MIDWARE_Nginx_keepaliverequests|keepalive_requests[{#port}]
#IT_RULE MIDWARE_Nginx_indexvaule|index_vaule[{#port}]
#IT_RULE MIDWARE_Nginx_proxypass|proxy_pass[{#port}]
#IT_END
#要输出脚本名称
shname=`basename $0`
ATTR="_X(g=$shname,p=cmdb,t=script,f=0)"
hostname=`hostname`
nginx_all=$(ps -ef | grep nginx | grep -v grep )
if [ "x$nginx_all" == "x" ];then exit 0; fi
if [ "$1" = "disc" ];then
ps aux | grep nginx | grep master | awk '{print $2}'|while read line
do
echo "{#port}="$line
done
exit 0
fi
cd /etc/sysconfig/network-scripts
ip_file=`ls ifcfg-* | grep -v lo | grep -v virbr |awk '{print $NF}'|head -1`
if [ -f $ip_file ];then
ip=`cat $ip_file | grep IPADDR | awk -F '=' '{print $2}'`
fi
if [ -z $ip ];then
if command -v ifconfig >/dev/null 2>&1; then
ip=`ifconfig -a | grep inet |grep -v 127.0.0.1 |grep -v inet6 |awk '{print $2}' |cut -d ':' -f 2 | tr "\n" "," |sed 's/.$//g'`
elif command -v ip >/dev/null 2>&1; then
ip=`ip addr show| grep inet |grep -v 127.0.0.1 | grep -v "broadcast 0.0.0.0" | grep -v inet6 |awk '{print $2}' |cut -d '/' -f 1 | tr "\n" "," |sed 's/.$//g'`
else
ip=`hostname -I|tr " " ","| awk '{print substr($0, 1, length($0)-1)}'`
fi
fi
pid=`ps aux | grep nginx | grep master | awk '{print $2}'`
nginx_conf_file=`ps -ef | grep nginx | grep -v grep | grep master | awk '{print $NF}'|head -1`
pattern='(/[^\]*nginx\.conf)'
if [[ ! -z ${nginx_conf_file} && $nginx_conf_file =~ $pattern ]];then
nginx_command_path=`ps -ef | grep nginx | grep -v grep | grep master |awk '{print $(NF-2)}'`
else
if [ -d /proc/${pid} ];then
nginx_command_path=`ls -al /proc/${pid}/exe |awk '{print $NF}'`
nginx_temp_path=`echo ${nginx_command_path} | awk -F'/sbin' '{print $1}'`
nginx_conf_file=`$nginx_command_path -t 2>&1 | grep -oE $pattern|sort|uniq`
else
echo "/proc/${pid}目录不存在,请检查进程"
exit 151
fi
fi
#nginx_path=`echo ${nginx_command_path} | awk -F'/sbin' '{print $1}'`
if [ -f ${nginx_conf_file} ];then
echo "${nginx_conf_file}文件存在,获取配置文件正常" >/dev/null
else
echo "${nginx_conf_file}文件不存在,获取配置文件失败,请检查"
exit 151
fi
#多指标输出开始
echo COL_DETAIL_START:
pidnum=`ps aux | grep nginx | grep -v grep | grep master| awk '{print $2}'|wc -l`
for((i=1;i<=$pidnum;i++));
do
${nginx_command_path} -v > /tmp/nginx_version.txt 2>&1
version=`cat /tmp/nginx_version.txt | awk -F "/" '{print $NF}'`
if command -v netstat >/dev/null 2>&1; then
port=`netstat -tlnup | grep nginx | awk -F "[ :]+" '{print $5}' | cut -d ':' -f 2 | tr "\n" "," | sed 's/.$//g'`
elif command -v ss >/dev/null 2>&1; then
port=`ss -tlnp | grep nginx | awk -F "[ :]+" '{print $5}' | cut -d ':' -f 2 | tr "\n" "," | sed 's/.$//g'`
else
echo "no netstat or ss命令"
fi
worker_num=`ps -ef | grep nginx | grep worker | wc -l`
start_user=`ps aux | grep nginx | grep master | awk '{print $1}'`
###客户端请求头部的缓冲区大小
header_buffer=$(cat ${nginx_conf_file} | grep client_header_buffer_size | grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g')
###客户请求头缓冲大小
large_buffer=$(cat ${nginx_conf_file} | grep large_client_header_buffer | grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $3}' |sed 's/.$//g')
###权限
###默认编码
default_charset=$(cat ${nginx_conf_file} | grep charset | grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g')
###参考事件模型
event_mode=$(cat ${nginx_conf_file} | grep ^use | grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g')
###服务器名字的hash表大小
hash_size=$(cat ${nginx_conf_file} | grep server_names_hash_bucket_size | grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}')
###活动连接超时时间
###所属应用系统名称
###通过ngix上传文件大小
nginx_max_size=$(cat ${nginx_conf_file} | grep client_max_body_size | grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}')
###错误日志目录
err_log=$(cat ${nginx_conf_file} | grep error_log | grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}')
###默认文件类型
default_file_type=$(cat ${nginx_conf_file} | grep default_type | grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g')
###长连接超时时间
keepalive_timeout=$(cat ${nginx_conf_file} | grep keepalive_timeout| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g')
###调度方式
#upstream_mode=$(cat ${nginx_conf_file} | grep -iE "weight|ip_hash|least_hash|url_hash"| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g') ###upstream后不是文本,需研究怎么取
#cat ${nginx_conf_file} | grep -iE "weight|ip_hash|least_hash|url_hash"| grep -v ^# |sed -n '/^[ ]*#/!p' |sed 's/.$//g'
touch /tmp/123.txt
for mode in weight ip_hash least_hash url_hash
do
cat ${nginx_conf_file} | grep -iE $mode >> /tmp/123.txt 2>&1
if [ $? -eq 0 ];then
upstream_mode=${mode}
#echo "${mode}"
else
continue
fi
done
###所属nginx的ip地址
server_ip=$(cat ${nginx_conf_file} | grep -w server| grep -E '[1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}'| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' | uniq |tr "\n" " ")
##cat ${nginx_conf_file} | grep -w server| grep -E '[1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}\.[1-9]{1,3}'| grep -v ^# |sed -n '/^[ ]*#/!p'
###server块中允许ip
allow_ip=$(cat ${nginx_conf_file} | grep allow| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g')
###server块中拒绝ip
deny_ip=$(cat ${nginx_conf_file} | grep deny| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g')
###server块中listen的值
listen_port=$(cat ${nginx_conf_file} | grep listen| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g' |tr "\n" " ")
###server块中根目录的值
root_path=$(cat ${nginx_conf_file} | grep root_path| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |sed 's/.$//g')
###server块中access_log日志
access_log=$(cat ${nginx_conf_file} | grep access_log| grep -v ^# |head -1|sed -n '/^[ ]*#/!p'| awk '{print $2}')
###server块中server_name
server_name=$(cat ${nginx_conf_file} | grep -w server_name| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}' |uniq |sed 's/.$//g')
###server块中keepalive_requests
keepalive_requests=$(cat ${nginx_conf_file} | grep keepalive_requests| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}'|sed 's/.$//g')
###server块中index
index_vaule=$(cat ${nginx_conf_file} | grep -w index| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $3}' |sed 's/;//g')
###标识符
###proxy_pass
proxy_pass=$(cat ${nginx_conf_file} | grep proxy_pass| grep -v ^# |sed -n '/^[ ]*#/!p'| awk '{print $2}'|sed 's/.$//g' |tr "\n" " ")
#输出脚本类型
echo "type[$ATTR,$pid]|+|NginxInfo"
echo "hostname[$ATTR,$pid]|+|$hostname"
echo "ip[$ATTR,$pid]|+|$ip"
echo "port[$ATTR,$pid]|+|$port"
echo "pid[$ATTR,$pid]|+|$pid"
echo "version[$ATTR,$pid]|+|${version##*/}"
echo "nginx_path[$ATTR,$pid]|+|$nginx_command_path"
echo "worker_num[$ATTR,$pid]|+|$worker_num"
echo "start_user[$ATTR,$pid]|+|$start_user"
echo "header_buffer[$ATTR,$pid]|+|$header_buffer"
echo "large_buffer[$ATTR,$pid]|+|$large_buffer"
echo "default_charset[$ATTR,$pid]|+|$default_charset"
echo "event_mode[$ATTR,$pid]|+|$event_mode"
echo "hash_size[$ATTR,$pid]|+|$hash_size"
echo "nginx_max_size[$ATTR,$pid]|+|$nginx_max_size"
echo "err_log[$ATTR,$pid]|+|$err_log"
echo "default_file_type[$ATTR,$pid]|+|$default_file_type"
echo "keepalive_timeout[$ATTR,$pid]|+|$keepalive_timeout"
echo "upstream_mode[$ATTR,$pid]|+|$upstream_mode"
echo "server_ip[$ATTR,$pid]|+|$server_ip"
echo "allow_ip[$ATTR,$pid]|+|$allow_ip"
echo "deny_ip[$ATTR,$pid]|+|$deny_ip"
echo "listen_port[$ATTR,$pid]|+|$listen_port"
echo "root_path[$ATTR,$pid]|+|$root_path"
echo "access_log[$ATTR,$pid]|+|$access_log"
echo "server_name[$ATTR,$pid]|+|$server_name"
echo "keepalive_requests[$ATTR,$pid]|+|$keepalive_requests"
echo "index_vaule[$ATTR,$pid]|+|$index_vaule"
echo "proxy_pass[$ATTR,$pid]|+|$proxy_pass"
done
#多指标输出结束
echo COL_DETAIL_END:
cat /dev/null > /tmp/123.txt 2>&1
运行结果如下图10.11.52.164
COL_DETAIL_START:
type[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|NginxInfo
hostname[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|zbx-server
ip[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|10.11.52.164
port[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|80
pid[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|1292
version[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|1.26.1
nginx_path[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|/usr/sbin/nginx
worker_num[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|4
start_user[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|root
header_buffer[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
large_buffer[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
default_charset[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
event_mode[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|nginx
hash_size[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
nginx_max_size[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
err_log[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|/var/log/nginx/error.log
default_file_type[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|application/octet-stream
keepalive_timeout[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|65
upstream_mode[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
server_ip[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
allow_ip[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
deny_ip[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
listen_port[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
root_path[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
access_log[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|/var/log/nginx/access.log
server_name[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
keepalive_requests[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
index_vaule[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
proxy_pass[_X(g=aa.sh,p=cmdb,t=script,f=0),1292]|+|
COL_DETAIL_END: