A few simple system monitoring scripts under Linux

Source: Internet
Author: User
Tags auth

A few simple system monitoring scripts under Linux

The company does not have a dedicated system administrator, so some server security measures also have to our programmer to do, the Linux server does not know a lot, check out some information, the following is a few of their own simple server monitoring script, hope to pass by the immortal pointing guidance, further correction and perfection!

1. Server login user monitoring, login users more than two email notification, use 139 mailbox to receive, convenient SMS notification.

Bash code:

#!/bin/bash

ip= ' Ifconfig eth0 | grep "inet addr" |awk ' {print $} ' |cut-f 2-d ': '

Users= ' Uptime|awk ' {print $6} '

#if [$users-ge 2]

If [$users-ge 2]

Then

echo "$IP Server login users are more than 2" |mail-s "Warning:" ****@139.com

Fi

2.MySQL Run status monitoring, restart service if not functioning properly, restart failed to send email notification

Bash code:

#! /bin/bash

#MySQL running this string is determined based on the information that is displayed when the database version is running correctly

/sbin/service MySQL Status | grep "MySQL Running" >/dev/null

If [$?-eq 0]

Then

#状态正常检查3306端口是否正常监听

NETSTAT-NTP | grep 3306 >/dev/null

If [$?-ne 0]

Then

/sbin/service MySQL Restart

Sleep 3

/sbin/service MySQL Status | grep "MySQL Running" >/dev/null

If [$?-ne 0]

Then

echo "MySQL service has stoped, Automatic startup failure, please start it manually!" | Mail-s "MySQL is not running" ***@139.com

Fi

Fi

Else

/sbin/service MySQL Start

Sleep 2;

/sbin/service MySQL Status | grep "MySQL Running" >/dev/null

If [$?-ne 0]

Then

echo "MySQL service has stoped, Automatic startup failure, please start it manually!" | Mail-s "MySQL is not running" ***@139.com

Fi

Fi

3. Hard disk space usage monitoring, when there is more than 80% partition space, mail notification

Bash code:

#!/bin/bash

#set-X

Checklog=/var/log/check-space.log

Fullflag=0

Df-h > $checkLog

percent_list=$ (cat $checkLog | awk ' {print $} ' | Grep-eo "[0-9]+")

For num in $percent _list

Do

If [$num-ge 80]; Then

Fullflag=1

Fi

Done

If [$fullFlag-eq 1]; Then

echo "$ (hostname): Used disk space is more than 80%" |mail-s "Warning:disk space was not enough" ***@139.com

Fi

PS: Mail is sent by configuring mail using an external SMTP server, where 163 of SMTP servers are used. VI edit/ETC/MAIL.RC, add the following code in the beginning section: Wq Exit Save!

XML code:

Set [email protected]

Set smtpsmtp=smtp.163.com

Set Smtp-auth-user=test

Set smtp-auth-password=test123

    • This article is from: Linux Learning Tutorial Network

A few simple system monitoring scripts under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.