数百台Server 批量巡检小插曲

来源:互联网 发布:golang 1.8.3 编辑:程序博客网 时间:2024/05/21 00:48
1、利用Xshell终端批量建同一用户并创建密码
useradd monitor
passwd 给予密码123
usermod -G root monitor
su - monitor
2、在统一消息控制器上进行生成公钥和私钥
ssh-keygen -t dsa----------一直回车
[monitor@localhost ~]$ ll -a
drwx------ 2 monitor monitor  4096 Apr 19 18:08 .ssh  目录权限为700
[monitor@localhost .ssh]$ ll -a
total 20
drwx------ 2 monitor monitor 4096 Apr 19 18:08 .
drwx------ 6 monitor monitor 4096 Apr 19 19:26 ..
-rw------- 1 monitor monitor  668 Apr 19 18:07 id_dsa-----私钥为600
-rw-r--r-- 1 monitor monitor  619 Apr 19 18:07 id_dsa.pub ---公钥为600
-rw-r--r-- 1 monitor monitor 1188 Apr 19 18:09 known_hosts
3、公钥上传到要巡检的服务器上
[monitor@localhost ~]$ cd .ssh/
[monitor@localhost .ssh]$ ls
id_dsa  id_dsa.pub  known_hosts
[monitor@localhost .ssh]$ ssh-copy-id -i id_dsa.pub monitor@192.168.100.52
4、客户端查看确认公钥已经存在,名称且为authorized_keys
monitor@localhost ~]$ cd .ssh/
[monitor@localhost .ssh]$ ls
authorized_keys
5、定义IP组写入文档,有1000个可定义1000个爽歪歪。。。
[monitor@localhost ~]$ cat iplist
192.168.100.52
192.168.100.53
192.168.100.54
......
6、写一脚本执行批量巡检工作,嘎嘎,批量执行
vim  /home/monitor/manger.sh
 #!/bin/bash
time=`date +"20%y%m%d"`
for ip in `cat /home/monitor/iplist`
        do
                echo "$ip-----------------">>/home/monitor/$ip.$time.txt
                Cpu1=`ssh -l monitor $ip top -b -n 1|grep Cpu|awk  '{print $5}'|awk -F "." '{print $1}'`
                Cpuus=`echo "100-$Cpu1"|bc`
                echo "#####################MEM################">>/home/monitor/server/$ip.$time.txt
                ssh -l monitor $ip free -m >>/home/monitor/server/$ip.$time.txt
                echo "#################DISK###################">>/home/monitor/server/$ip.$time.txt
                ssh -l monitor $ip df -h >>/home/monitor/server/$ip.$time.txt
                echo "your Cpu is used $Cpuus%">>//home/monitor/server/$ip.$time.txt
                wait
done
7、写一计划任务,每天早上定期执行,要巡检的内容
crontab -e
 0 7 * * * /bin/sh  /home/monitor/manger.sh
8、测试TEST
[monitor@localhost server]$ ls
192.168.100.52.20140419.txt  192.168.100.53.20140419.txt  192.168.100.54.20140419.txt
[monitor@localhost server]$
[monitor@localhost server]$ cat 192.168.100.52.20140419.txt
#####################MEM################
             total       used       free     shared    buffers     cached
Mem:           249        235         13          0         15        169
-/+ buffers/cache:         51        198
Swap:         2047          0       2047
#################DISK###################
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              18G  2.2G   15G  14% /
/dev/sda1             289M   16M  259M   6% /boot
tmpfs                 125M     0  125M   0% /dev/shm
your Cpu is used 3%
[monitor@localhost server]$
总结,上述还可以结合公钥和私钥的特性进行统一备份,统一查看备份内容。
构思总结:可以把 要备份的服务器私钥上传到统一备份服务器上,公钥留在本地,直接推送要备份内容即可。

优化空间非常大,望经验多的大神们多多指点,,

 
0 0
原创粉丝点击