[转帖]Introduction to Linux monitoring and alerting
Introduction to Linux monitoring and alerting
https://www.redhat.com/sysadmin/linux-monitoring-and-alerting mail 的命令挺有意思的.
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.
More Linux resources
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的更多相关文章
- 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 ...
- (转帖整理)Linux下的Autoconf和AutoMake(理论篇) 1
在搜索网上资料过程中,这是感觉最简洁有效的一篇文章,特进行转帖记录,并根据情况对部分内容进行了修改.原帖传送门:Linux下的Autoconf和AutoMake 1.工具安装在开始使用autoconf ...
- Introduction to Linux Threads
Introduction to Linux Threads A thread of execution is often regarded as the smallest unit of proces ...
- [转帖]Introduction to text manipulation on UNIX-based systems
Introduction to text manipulation on UNIX-based systems https://www.ibm.com/developerworks/aix/libra ...
- [转帖]NotePad++编辑Linux中的文件
NotePad++编辑Linux中的文件 https://blog.csdn.net/chengqiuming/article/details/78882692 原作者 未经允许不允许转帖 加密自己参 ...
- [转帖]Windows和Linux对决(多进程多线程)
Windows和Linux对决(多进程多线程) https://blog.csdn.net/world_2015/article/details/44920467 太长了 还没看完.. 还是没太理解好 ...
- 【转帖】理解 Linux 的虚拟内存
理解 Linux 的虚拟内存 https://www.cnblogs.com/zhenbianshu/p/10300769.html 段页式内存 文章了里面讲了 页表 没讲段表 记得最开始的时候 学习 ...
- [转帖]新的Linux后门开始肆虐 主要攻击中国服务器
新的Linux后门开始肆虐 主要攻击中国服务器 https://www.cnbeta.com/articles/tech/815639.htm 一种新的 Linux 系统后门已经开始肆虐,并主要运行在 ...
- []转帖] 浅谈Linux下的五种I/O模型
浅谈Linux下的五种I/O模型 https://www.cnblogs.com/chy2055/p/5220793.html 一.关于I/O模型的引出 我们都知道,为了OS的安全性等的考虑,进程是 ...
随机推荐
- [Javascript] Keyword 'in' to check prop exists on Object
function addTo80(n ) { + n; } function memoizedAddTo80 (fn) { let cache = {}; return (n) => { /*k ...
- [Web] About image: MozJPEG
Image is quite heavy in web traffic. it is about 53% whole web traffic. It is important to make sure ...
- 关于Windows系统里的事后调试
我一直在想,应用程序抛出未处理的异常和附加到进程的调试器之间会发生什么.显然这些信息就在我眼皮底下,但我是瞎子.Windows调试器关于事后调试的文档包含了您想要知道的所有详细信息. 最常见的应用程序 ...
- pgloader 学习(九) pg 2 pg 使用with 参数控制同步逻辑
pgloader 支持比较丰富的配置参数,同时默认数据在同步的时候是会进行索.schema 以及数据的同步对于实际我们可能存在需要进行控制,我们可以通过with 参数方便的处理 参考配置 load 文 ...
- ipcs
用于报告Linux中进程间通信设施的状态,显示的信息包括消息列表.共享内存和信号量的信息
- Echarts的简单入门
5 分钟上手 ECharts 获取 ECharts 你可以通过以下几种方式获取 ECharts. 从官网下载界面选择你需要的版本下载,根据开发者功能和体积上的需求,我们提供了不同打包的下载,如果你在体 ...
- JS filter的使用
定义和用法 filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素. 注意: filter() 不会对空数组进行检测. 注意: filter() 不会改变原始数组 ...
- 多维矩阵转一维数组(c++)【转载】
在由二维矩阵转为一维数组时,我们有两种方式:以列为主和以行为主. 以列为主的二维矩阵转为一维数组时,转换公式为: index=column+row×行数 以行为主的二维矩阵转为一维数组时,转换公式为: ...
- px,em和rem
1 px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的 2 em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字 ...
- 微信小程序之使用wx:for遍历循环
效果图如下: 实现代码如下:type.js: // pages/type/type.js Page({ /** * 页面的初始数据 */ data: { types: "" }, ...