Linux Shell脚本专栏_监控100台服务器磁盘利用率脚本_07

监控100台服务器磁盘利用率脚本

 1. df -h 查看磁盘占有
 2. 远程连接

1. 远程连接生成sshkey

[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
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:
SHA256:oqRrNpiEGm/lvN6Nark2VQHckCk56Egda/RVeG1J+qc [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|  .+.ooBo.o..    |
| .o.* =.o..+     |
|.o o +  .o.      |
|. o     . .      |
|.   . ..S  . .   |
|o. o....    o    |
|o=.+.o     E     |
|+ *.B. o         |
| +.===o .        |
+----[SHA256]-----+
[root@localhost ~]#

2. 复制ssh公钥到目标服务器

[root@localhost ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.43.228 (192.168.43.228)' can't be established.
ECDSA key fingerprint is SHA256:FXF2nnf+x1mP2M662486Z/51fjmcwfE23S2Pewwlxx8.
ECDSA key fingerprint is MD5:c0:cb:5f:3f:ba:3a:b4:2b:73:47:7b:59:d1:79:94:17.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

3. 登录目标服务器查看

192.168.43.228

[root@localhost ~]# ls -a
.   anaconda-ks.cfg  .bash_profile  .cshrc  .tcshrc
..  .bash_logout     .bashrc        .ssh    .viminfo
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ll
total 4
-rw------- 1 root root 408 Feb 24 21:16 authorized_keys
[root@localhost .ssh]#

在这里插入图片描述

4. 私钥免登录

192.168.43.134 服务器通过私钥免交互登录目标服务器(192.168.43.228)

[root@localhost ~]# ssh -i .ssh/id_rsa [email protected]
Last login: Mon Feb 24 21:20:34 2020 from 192.168.43.134
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.228  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::2df:a3c9:3a0d:ed24  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:07:3f:59  txqueuelen 1000  (Ethernet)
        RX packets 1602  bytes 151986 (148.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1035  bytes 140376 (137.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 296  bytes 54872 (53.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 296  bytes 54872 (53.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]#

5. 192.168.43.134 服务器创建host,info文件

vim host.info

192.168.43.228 root 22
192.168.43.226 ly   22
192.168.43.225 user 22

6. 编辑脚本


#!/bin/bash
HOST_INFO=host.info
for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
    USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
    PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
    TMP_FILE=/tmp/disk.tmp
    ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE
    USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $NF,int($5)}' $TMP_FILE)
    for USE_RATE in $USE_RATE_LIST; do
        PART_NAME=${USE_RATE%=*}
        USE_RATE=${USE_RATE#*=}
        if [ $USE_RATE -ge 80 ]; then
            echo -e "$IP /n Warning: $PART_NAME Partition usage $USE_RATE%!"
        else
           echo "SERVER OK!"
        fi
    done
done

7. 赋予可执行权限

chmod +x 6.sh

8. 运行脚本

[root@localhost app]# ./6.sh
SERVER OK!
SERVER OK!
SERVER OK!

9. 命令分解

[root@localhost ~]# df -h |awk '/^\/dev/{print $0}'
/dev/mapper/centos-root   50G  2.1G   48G   5% /
/dev/sda1               1014M  146M  869M  15% /boot
/dev/mapper/centos-home   47G   33M   47G   1% /home
vim host.info

192.168.43.228 root 22
192.168.43.226 ly   22
192.168.43.225 user 22
#$1 第1列
#$2 第2列
#$3 第3列
[root@localhost ~]# awk '/^[^#]/{print $1}' host.info 
192.168.43.228
192.168.43.226
192.168.43.225
[root@localhost ~]# awk '/^[^#]/{print $2}' host.info 
root
ly
user
[root@localhost ~]# awk '/^[^#]/{print $3}' host.info 
22
22
22

在这里插入图片描述

[root@localhost ~]# awk -v ip=192.168.43.228 '$1==ip{print $2}' host.info root

在这里插入图片描述

发布了858 篇原创文章 · 获赞 114 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/weixin_40816738/article/details/104483531