给我写一个监控nginx服务状态额脚本
时间: 2023-02-08 18:43:47 浏览: 117
下面是一个使用 bash 脚本监控 Nginx 服务状态的示例:
```
#!/bin/bash
# 检查 Nginx 进程是否存在
if ps -ef | grep -v grep | grep nginx ; then
echo "Nginx is running."
else
echo "Nginx is not running."
fi
```
这个脚本会检查进程列表中是否有 Nginx 的进程在运行,如果有就输出"Nginx is running.",如果没有就输出"Nginx is not running."
注意:这个脚本只能检测nginx是否运行,并不能检测nginx的其他状态,比如说连接数,负载等等,如果需要检测这些信息,可以使用nginx的stub_status模块.
相关问题
给我写一个监控nginx的shell脚本
这个脚本可以监控nginx服务器的运行状态:
#!/bin/bash# 检查nginx进程
# 如果不存在,则启动nginx
if ! pgrep nginx &>/dev/null; then
echo "Nginx process not found, starting nginx.."
sudo service nginx start
fi# 检查nginx进程是否正常运行
# 如果不正常,则重新启动nginx
if [[ $(systemctl is-active nginx) != "active" ]]; then
echo "Nginx process is not active, restarting nginx.."
sudo service nginx restart
fi
帮我写一个 centos 7 zabbix 监控nginx的脚本
好的,我可以帮你写一个监控nginx的脚本,请问你已经安装了zabbix及其相关插件吗?如果还没有,请先安装好。下面是一个简单的监控nginx负载的脚本示例:
```bash
#!/bin/bash
# Author: your name
# Date: YYYY-MM-DD
# Description: Monitor Nginx status for Zabbix
# Nginx status page URL
URL="http://localhost/nginx_status"
# Get nginx active connections
active_connections=$(curl -s $URL | awk '/Active/{print $NF}')
# Get nginx accepted connections
accepted_connections=$(curl -s $URL | awk 'NR==3{print $1}')
# Get nginx handled connections
handled_connections=$(curl -s $URL | awk 'NR==3{print $2}')
# Get nginx total requests
total_requests=$(curl -s $URL | awk 'NR==3{print $3}')
# Get nginx reading connections
reading=$(curl -s $URL | awk '/Reading/{print $2}')
# Get nginx writing connections
writing=$(curl -s $URL | awk '/Writing/{print $4}')
# Get nginx waiting connections
waiting=$(curl -s $URL | awk '/Waiting/{print $6}')
# Send data to zabbix
echo "nginx.active_connections $active_connections $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i -
echo "nginx.accepted_connections $accepted_connections $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i -
echo "nginx.handled_connections $handled_connections $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i -
echo "nginx.total_requests $total_requests $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i -
echo "nginx.reading_connections $reading $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i -
echo "nginx.writing_connections $writing $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i -
echo "nginx.waiting_connections $waiting $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i -
```
你需要将脚本中的$URL替换为你实际的nginx状态页面URL,并将$zabbix_server_ip和$nginx_server_name替换为你的zabbix服务器IP和nginx服务器名称。该脚本可以使用zabbix_sender命令将收集的数据发送到zabbix服务器,以便在zabbix监控界面中查看nginx的实时状态和趋势。
阅读全文