-->
当前位置:首页 > 题库

主观题:h0013.有10台被监控主机、一台监控机,在监控机上编写脚本,一旦某台被监控机器/分区适用率大于80%,就发邮件报警放到crontab 里面,每十分钟检查一次

Luz4年前 (2022-10-05)题库234
有10 台被监控主机、一台监控机,在监控机上编写脚本,一旦某台被监控机器/分区适用率大于80%, 就发邮件报警放到crontab 里面, 每10 分钟检查一次。





答案:#测试机器:虚拟机Linux as 4

#1.首先建立服务器间的信任关系。拿两台机器做测试

本机ip:192.168.1.6

[root@codfei ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

/root/.ssh/id_rsa already exists.

Overwrite (y/n)? y (因为我是第2 次建立关系所以此处覆盖原来的文件)

Enter passphrase (empty for no passphrase):(直接回车无须输入密钥)

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

04:37:13:2a:4b:10:af:c1:2b:03:3f:6b:27:ce:b9:62 root@codfei

[root@codfei ~]# cd .ssh/

[root@codfei .ssh]# ll

-rw------- 1 root root 883 Apr 25 17:51 id_rsa

-rw-r--r-- 1 root root 221 Apr 25 17:51 id_rsa.pub

-rw-r--r-- 1 root root 442 Apr 25 17:37 known_hosts

id_rsa 是密钥文件,id_rsa.pub 是公钥文件。

[root@codfei .ssh]# scp id_rsa.pub192.168.1.4:/root/.ssh/192.168.1.6

root@192.168.1.4's password:

id_rsa.pub 100% 221 0.2KB/s 00:00

这里把公钥文件取名为本机的ip 地址就是为了以后和更多的机器建立信任关系不发生混淆。

现在登陆到192.168.1.4 机器

[root@codfei ~]# cd .ssh/

[root@codfei .ssh]# cat 192.168.1.6 >> authorized_keys

然后回到192.168.1.6 机器直接

[root@codfei .ssh]# ssh 192.168.1.4

Last login: Wed Aug 8 12:14:42 2007 from 192.168.1.6

这样就可以了,里面偶尔涉及到权限问题。一般./ssh 文件夹是755 authorized_keys 为600 或者644

####脚本如下#######################

#!/bin/bash

#SCRIPT:df_check.sh

#Writeen by codfei Mon Sep 3 07:25:28 CST 2007

#PURPOSE:This script is used to monitor for full filesystems.

#######################Begining####################

####################

FSMAX="80"

remote_user='root' #####完全可以不用root

remote_ip=(192.168.1.5 192.168.1.6 192.168.1.7 192.168.1.8 192.168.1.9

192.168.1.10 192.168.1.11 192.168.1.12 192.168.1.13 192.168.1.14 ) ---->

这里填写你要监控的主机ip

ip_num='0'

while [ "$ip_num" -le "$(expr ${#remote_ip[@]} - 1)" ]

do

read_num='1'

ssh "$remote_user"@"${remote_ip[$ip_num]}" df -h > /tmp/diskcheck_tmp

grep '^/dev/*' /tmp/diskcheck_tmp|awk '{print $5}'|sed 's/\%//g' >

/tmp/diskcheck_num_tmp

while [ "$read_num" -le $(wc -l < /tmp/diskcheck_num_tmp) ]

do

size=$(sed -n "$read_num"'p' /tmp/diskcheck_num_tmp)

if [ "$size" -gt "$FSMAX" ]

then

$(grep '^/dev/*' /tmp/diskcheck_tmp|sed -n $read_num'p' >

/tmp/disk_check_mail)

$(echo ${remote_ip[$ip_num]} >> /tmp/disk_check_mail)

$(mail -s "diskcheck_alert" admin < /tmp/disk_check_mail)

fi

read_num=$(expr $read_num + 1)

done

ip_num=$(expr $ip_num + 1)

done

#############over################################

################让脚本每十分钟执行一次#############
在cron 表中加入
0/10 * * * * /home/codfei/diskcheck.sh 2>&1
################################################
##########################