Introduction to Linux monitoring and alerting

https://www.redhat.com/sysadmin/linux-monitoring-and-alerting

mail 的命令挺有意思的. 
Have you ever wanted to set up a process monitor that alerts you when it's offline without spending thousands of budget dollars to do so? Every system administrator has, and here's how to do it.

Posted October 3, 2019 | byKen Hess (Red Hat)

There are system administrators who love to do things themselves, and then there are those of us who must do things ourselves because budgets just don't allow for mega-purchases. Enterprise monitoring and alerting suites are for companies that either have large budgets or for those that have mission-critical applications, systems, or services that absolutely must be up 100% of the time. There are some open-source monitoring and alerting suites, but they require a dedicated system and a considerable amount of time to set up. Most also require agents to be installed on monitored endpoints, which requires approval and time to deploy. The quicker and easier solution is to create your own monitoring and alerting scripts and then schedule them via cron. The best part of localized (per server) monitoring and alerting is that you can customize thresholds for each system and service, rather than having to live with a global configuration that might not meet your needs.

This article takes you through the process of creating a script that checks every five minutes for the Apache web server process, attempts to restart it if it's down, and then alerts you via email if it's down for more than 30 seconds and cannot be restarted.

Most processes have a process ID (PID) file under the /run directory when they are running, and many of those have their own separate directories that contain their corresponding PID files. In this example, the Apache web server (httpd) has a PID file: /run/httpd/httpd.pid.

I named this script  apache.sh , and placed it into root's home directory. Be sure to change permissions on the file to 750 ( rwxr -x---) so that no other user can execute or even read this file, regardless of location:

$ sudo chmod 750 apache.sh

Note: If you don't have Apache installed, it doesn't matter, because you can replace the httpd.pid file pointed to in the script with any other PID file that works for your system.

There are many different ways to create such a script, but this is how I did it, and it works. I identified the PID file with the variable, FILE. I decided that rather than have an alert sent if the Apache web server was down, I would have the script attempt a service restart, and then check again. I repeated this process two more times, waiting for 10 seconds between checks. If the Apache service is still down and cannot be restarted after 30 seconds, then the script sends the system administrator team an email:

#!/bin/bash

FILE=/run/httpd/httpd.pid

if ! [ -f "$FILE" ]; then
systemctl start httpd.service
fi
sleep 10s
if ! [ -f "$FILE" ]; then
systemctl start httpd.service
fi
sleep 10s
if ! [ -f "$FILE" ]; then
systemctl start httpd.service
fi
sleep 10s
if ! [ -f "$FILE" ]; then
mail -s 'Apache is down' sysadmins@mydomain.com <<< 'Apache is down on SERVER1 and cannot be restarted'
fi

You could just as easily send an SMS message to a team on-call mobile phone. This script checks for the non-existence of the httpd.pid file and then takes action if it's not found. If the file exists, then no action is taken. No one wants to receive emails or notices that a service is up every five minutes.

Once you've tested your script and satisfied that it operates as desired, place this script into the root user's crontab:

$ sudo crontab -e

The entry I've made below runs the script every five minutes:

*/5 * * * * /root/apache.sh

This script is an example of a quick method for setting up a process monitor and alert on a local system. Yes, it's primitive and simple, but it works and it's free. It also doesn't require any budget discussions, nor does it require a maintenance window for agent installation. You'll also find that this script doesn't significantly impact performance on your system. These are all good things. And if you're an Ansible administrator, you could deliver this script to your entire fleet of systems without having to touch each one individually.

Want to learn more advanced techniques for monitoring in Linux? Check out The open source guide to DevOps monitoring tools.

[转帖]Introduction to Linux monitoring and alerting的更多相关文章

  1. A Quick Introduction to Linux Policy Routing

    A Quick Introduction to Linux Policy Routing 29 May 2013 In this post, I’m going to introduce you to ...

  2. (转帖整理)Linux下的Autoconf和AutoMake(理论篇) 1

    在搜索网上资料过程中,这是感觉最简洁有效的一篇文章,特进行转帖记录,并根据情况对部分内容进行了修改.原帖传送门:Linux下的Autoconf和AutoMake 1.工具安装在开始使用autoconf ...

  3. Introduction to Linux Threads

    Introduction to Linux Threads A thread of execution is often regarded as the smallest unit of proces ...

  4. [转帖]Introduction to text manipulation on UNIX-based systems

    Introduction to text manipulation on UNIX-based systems https://www.ibm.com/developerworks/aix/libra ...

  5. [转帖]NotePad++编辑Linux中的文件

    NotePad++编辑Linux中的文件 https://blog.csdn.net/chengqiuming/article/details/78882692 原作者 未经允许不允许转帖 加密自己参 ...

  6. [转帖]Windows和Linux对决(多进程多线程)

    Windows和Linux对决(多进程多线程) https://blog.csdn.net/world_2015/article/details/44920467 太长了 还没看完.. 还是没太理解好 ...

  7. 【转帖】理解 Linux 的虚拟内存

    理解 Linux 的虚拟内存 https://www.cnblogs.com/zhenbianshu/p/10300769.html 段页式内存 文章了里面讲了 页表 没讲段表 记得最开始的时候 学习 ...

  8. [转帖]新的Linux后门开始肆虐 主要攻击中国服务器

    新的Linux后门开始肆虐 主要攻击中国服务器 https://www.cnbeta.com/articles/tech/815639.htm 一种新的 Linux 系统后门已经开始肆虐,并主要运行在 ...

  9. []转帖] 浅谈Linux下的五种I/O模型

    浅谈Linux下的五种I/O模型 https://www.cnblogs.com/chy2055/p/5220793.html  一.关于I/O模型的引出 我们都知道,为了OS的安全性等的考虑,进程是 ...

随机推荐

  1. TypeScript语言学习笔记(4)枚举

    枚举 // 数值型枚举 enum Direction { Up = 1, Down, Left, Right, } // Up=0 enum Direction { Up, Down, Left, R ...

  2. Selenium ChromeDriver与Chrome版本映射表(更新到v77)

    ChromeDriver版本 支持的Chrome版本 v77.0.3865.40 v77 v76.0.3809.126 v76 v75.0.3770.140 v75 v74 v74 v73 v73 v ...

  3. Lightning Web Components 组件样式(四)

    要将样式与组件进行绑定,需要创建一个同名的样式文件,这样样式将会自动应用到组件 在组件中定义的样式的作用域是属于组件的,这样允许组件可以在不同的上下文中可以复用, 可以阻止其他组件的样式的复写 css ...

  4. c语言中一种典型的排列组合算法

    c语言中的全排列算法和组合数算法在实际问题中应用非常之广,但算法有许许多多,而我个人认为方法不必记太多,最好只记熟一种即可,一招鲜亦可吃遍天 全排列: #include<stdio.h> ...

  5. System.getProperty、PropConfig.loadConfig应用

    1.获取项目下制定位置(System.getProperty("admin.root")): 在web.xml中配置webAppRootKey <context-param& ...

  6. Java基础教程(全代码解析)

    字面量: 整数字面量为整型(int) 小数字面量为双精度浮点型(double) 数据类型: byte short int long float double 接下来代码展示理解 public clas ...

  7. mysql 分组和排序

    mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...

  8. Jmeter(四十二)_控制器下遍历一组参数

    概述 在接口自动化的过程中,经常遇到需要遍历的参数组.jmeter在中,foreach控制器可以实现遍历参数,但是只能有一个入参.一旦遇到数组,foreach控制器表示我也无能为力... 为了解决这个 ...

  9. 关于SkyApm测试部署。

    这个是skyapm的github : https://github.com/SkyAPM/SkyAPM-dotnet 它依赖于skywalking . 我是用docker去部署的.因为这样我的系统会干 ...

  10. 【大数据】安装关系型数据库MySQL安装大数据处理框架Hadoop

    作业来源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3161 1. 简述Hadoop平台的起源.发展历史与应用现状. 列举发展过 ...