当前位置: 首页 > news >正文

Shell脚本学习-Web服务监控

参考我的博客文章《Centos安装nginx》,先来安装下nginx。我按照该文档操作了一遍,还是很快就能安装好nginx的。

确认可以安装成功:

[root@vm1 sbin]# netstat -atunlp |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21573/nginx: master[root@vm1 html]# echo changchunhua > index.html
[root@vm1 html]#
[root@vm1 html]# curl http://localhost
changchunhua

1、端口

本地:netstat ss lsof

[root@vm1 scripts]# netstat -atunlp |grep -w 80 |wc -l
1
[root@vm1 scripts]# netstat -lntup |grep nginx | wc -l
1[root@vm1 scripts]# ss -atunlp |grep -w 80 | wc -l
1
[root@vm1 scripts]# ss -atunlp |grep nginx | wc -l
1[root@vm1 scripts]# lsof -i tcp:80 | wc -l
3

远程:telnet nmap nc

[root@vm1 scripts]# nmap localhost -p 80Starting Nmap 6.40 ( http://nmap.org ) at 2023-08-02 00:17 CST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000040s latency).
Other addresses for localhost (not scanned): 127.0.0.1
PORT   STATE SERVICE
80/tcp open  httpNmap done: 1 IP address (1 host up) scanned in 0.23 seconds
[root@vm1 scripts]# nmap 127.0.0.1 -p 80 |grep open | wc -l
1
[root@vm1 scripts]# echo -e "\n" | telnet 127.0.0.1 80 2>/dev/null
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
[root@vm1 scripts]# echo -e "\n" | telnet 127.0.0.1 80 2>/dev/null |grep Connected | wc -l
1
[root@vm1 scripts]# nc -z -v 127.0.0.1 80
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 127.0.0.1:80.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.[root@vm1 scripts]# nc -z -v 127.0.0.1 80 &>/dev/null
[root@vm1 scripts]# echo $?
0

2、本地进程数:

[root@vm1 scripts]# ps -ef |grep nginx |grep -v grep
root      21573      1  0 00:01 ?        00:00:00 nginx: master process ./nginx
nobody    21574  21573  0 00:01 ?        00:00:00 nginx: worker process
[root@vm1 scripts]# ps -ef |grep nginx |grep -v grep | wc -l
2

3、header(http code) curl -I

4、URL(wget curl)

客户端模拟用户访问的监控方式,先通过wget和curl命令进行测试,执行wget和curl命令之后,再看返回值$?,为0,则为成功。

[root@vm1 scripts]# wget --spider --timeout=10 --tries=2 http://127.0.0.1 &>/dev/null
[root@vm1 scripts]# echo $?
0

获取字符串的方式:

[root@vm1 scripts]# curl http://127.0.0.1
changchunhua

根据HTTP响应header的结果进行判断:

[root@vm1 scripts]# curl -I -s -w "%{http_code}\n" -o /dev/null http://127.0.0.1
200

然后我们可以写出脚本:

示例1:

[root@vm1 scripts]# cat nginx_monitor1.sh
#!/bin/bash
#if [ `netstat -auntlp |grep nginx | wc -l` -gt 0 ]thenecho "nginx is running."
elseecho "Nginx is stopped."/usr/local/nginx/sbin/nginx
fi
[root@vm1 scripts]# sh nginx_monitor1.sh
nginx is running.
[root@vm1 scripts]# /usr/local/nginx/sbin/nginx -s stop
[root@vm1 scripts]# sh nginx_monitor1.sh
Nginx is stopped.
[root@vm1 scripts]# sh nginx_monitor1.sh
nginx is running.
[root@vm1 scripts]#

示例2:

[root@vm1 scripts]# cat nginx_monitor2.sh
#!/bin/bash
#if [ `lsof -i:80 | wc -l` -gt 0 ]thenecho "nginx is running."
elseecho "Nginx is stopped."/usr/local/nginx/sbin/nginx
fi[root@vm1 scripts]# sh nginx_monitor2.sh
nginx is running.
[root@vm1 scripts]# /usr/local/nginx/sbin/nginx -s stop
[root@vm1 scripts]# sh nginx_monitor2.sh
Nginx is stopped.
[root@vm1 scripts]# sh nginx_monitor2.sh
nginx is running.
[root@vm1 scripts]# lsof -i:80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   21771   root    6u  IPv4  37072      0t0  TCP *:http (LISTEN)
nginx   21772 nobody    6u  IPv4  37072      0t0  TCP *:http (LISTEN)

示例3:

[root@vm1 scripts]# cat nginx_monitor3.sh
#!/bin/bash
#if [ `ps -ef |grep nginx | grep -v grep | wc -l` -gt 2 ]thenecho "nginx is running."
elseecho "Nginx is stopped."/usr/local/nginx/sbin/nginx
fi

说明:

1)为什么要进程数要大于2呢?

在调试的过程中只有改成-gt 2才能启动nginx。过滤进程方式,排除自身。

http://www.lryc.cn/news/105026.html

相关文章:

  • 【ChatGPT】基于WSL+Docker的ChatGPT PLUS共享服务部署
  • 【论文阅读24】Better Few-Shot Text Classification with Pre-trained Language Model
  • 119、Spring容器启动流程是怎样的(配有Spring启动完整流程图)
  • 微信公众号开发学习
  • 【LeetCode】221.最大正方形
  • 生成模型相关算法:EM算法步骤和公式推导
  • Compose手势
  • 【雕爷学编程】Arduino动手做(177)---ESP-32 掌控板2
  • Ubuntu-文件和目录相关命令
  • 显式接口实现(C# 编程指南)
  • element-ui 图片上传 及 quillEditor富文本(图片视频上传)
  • 前端技术Vue学习笔记--002
  • 【RabbitMQ(day4)】SpringBoot整合RabbitMQ与MQ应用场景说明
  • 想了解好用的翻译pdf的软件吗?
  • docker安装nginx并配置SSL
  • 【LeetCode 算法】Reorder List 重排链表
  • MQ面试题3
  • 【Linux命令200例】patch 用于将补丁文件应用到源码中
  • 一起来学算法(邻接矩阵)
  • hadoop与HDFS交互
  • MYSQL 分区如何指定不同存储路径(多块磁盘)
  • 安全加固服务器
  • Linux 命令学习:
  • 牛客网Verilog刷题——VL54
  • 学习系统编程No.34【线程同步之信号量】
  • SolidUI社区-Snakemq 通信源码分析
  • 【大数据之Flume】四、Flume进阶之复制和多路复用、负载均衡和故障转移、聚合案例
  • 前端学习--vue2--插槽
  • 使用 Docker Compose 部署 Redis Cluster 集群,轻松搭建高可用分布式缓存
  • 在Spring Boot框架中集成 Spring Security