Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1538275
  • 博文数量: 416
  • 博客积分: 10061
  • 博客等级: 上将
  • 技术积分: 3287
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-05 11:12
个人简介

技术在于专研

文章分类

全部博文(416)

文章存档

2021年(3)

2015年(34)

2013年(2)

2012年(1)

2011年(2)

2010年(5)

2007年(344)

2006年(25)

分类: 系统运维

2021-06-28 10:19:39

由于一些特殊情况,不允许安装监控平台,为了巡检方便自己写了python脚本

python代码:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*-coding:utf8 -*-
  3. # @Time : 2020/12/11 11:15
  4. # @Author : riches
  5. # @Site :
  6. # @File : hnxj.py
  7. # @Software: PyCharm
  8. # -*- coding: utf-8 -*-
  9. import os, sys

  10. reload(sys)
  11. sys.setdefaultencoding('utf8')
  12. import paramiko
  13. import xlsxwriter
  14. import time


  15. def sshexeccmd(ip):
  16.     tmplist = {}
  17.     tmplist["ip"] = ip
  18.     tmplist["cpuuse"] = None
  19.     tmplist["momeryall"] = None
  20.     tmplist["momeryuse"] = None
  21.     tmplist["diskall"] = None
  22.     tmplist["diskuse"] = None
  23.     tmplist["homeall"] = None
  24.     tmplist["homeuse"] = None
  25.     tmplist["datause"] = None
  26.     tmplist["flowuse"] = None
  27.     tmplist["flow1use"] = None
  28.     try:
  29.         ssh = paramiko.SSHClient()
  30.         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  31.        pkey = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
  32.         #pkey = paramiko.RSAKey.from_private_key_file('C:\\Users\\riches\\.ssh\\id_rsa')#Windows系统写法

  33.         # ssh.connect(hostname=ip, port=22, username='root', pkey=pkey)
  34.         ssh.connect(hostname=ip, port=22, username='xunjian', pkey=pkey)#建立专用巡检账号避免root账号直接登录
  35.         # stdin, stdout, stderr = ssh.exec_command(cmd)#命令基本格式
  36.         stdincpu, stdoutcpu, stderrcpu = ssh.exec_command(
  37.             "top -b -n 1 | head -n 4 | grep 'Cpu(s)' | awk '{print $2}' | cut -d 'u' -f 1")
  38.         stdinmomeryall, stdoutmomeryall, stderrmomeryall = ssh.exec_command(
  39.             "free -m | awk '{print $2}' | awk 'NR==2{print}'")
  40.         stdinmomery, stdoutmomery, stderrmomery = ssh.exec_command("echo `free -m | sed -n '2p' | awk '{print ($2-$7)/$2*100}'|awk -F'.' '{print $1}'`%")
  41.         stdindiskall, stdoutdiskall, stderrdiskall = ssh.exec_command("df -hP | awk '/\/$/ {print $2}'")
  42.         stdindisk, stdoutdisk, stderrdisk = ssh.exec_command("df -hP | awk '/\/$/ {print $5}'")
  43.         stdinhomeall, stdouthomeall, stderrhomeall = ssh.exec_command(
  44.             "df -hP /home | awk '{print $2}' | awk 'NR==2{print}'")
  45.         stdinhome, stdouthome, stderrhome = ssh.exec_command(
  46.             "df -hP /home | awk '{print $5}' |awk 'NR==2{print}'")
  47.         stdindata, stdoutdata, stderrdata = ssh.exec_command(
  48.             "df -hP /data | awk '{print $5}' | awk 'NR==2{print}'")
  49.         stdinflow, stdoutflow, stderrflow = ssh.exec_command(
  50.             "pescli localhost show rate|grep eth2 |awk -F: '{print $2}'|awk 'NR==1{print $1}'| sed s'/M,//'")
  51.         stdinflow1, stdoutflow1, stderrflow1 = ssh.exec_command(
  52.             "pescli localhost show rate|grep eth4 |awk -F: '{print $2}'|awk 'NR==1{print $1}'| sed s'/M,//' ")

  53.         tmplist["ip"] = ip
  54.         tmplist["cpuuse"] = (stdoutcpu.read()).replace("\n", "")
  55.         tmplist["momeryall"] = (stdoutmomeryall.read()).replace("\n", "")
  56.         tmplist["momeryuse"] = (stdoutmomery.read()).replace("\n", "")
  57.         tmplist["diskall"] = (stdoutdiskall).read().replace("\n", "")
  58.         tmplist["diskuse"] = (stdoutdisk.read()).replace("\n", "")
  59.         tmplist["homeall"] = (stdouthomeall).read().replace("\n", "")
  60.         tmplist["homeuse"] = (stdouthome).read().replace("\n", "")
  61.         tmplist["datause"] = (stdoutdata).read().replace("\n", "")
  62.         tmplist["flowuse"] = (stdoutflow).read().replace("\n", "")
  63.         tmplist["flow1use"] = (stdoutflow1).read().replace("\n", "")

  64.         print tmplist
  65.         return tmplist

  66.         ssh.close()
  67.     except Exception, e:
  68.         print e
  69.         return tmplist


  70. def trywexrestr(lists):
  71.     nowtime = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) + ""
  72.     bookurl = "systems" + nowtime + 'info.xlsx'
  73.     # filenames=nowtime+'info.xlsx'
  74.     workbook1 = xlsxwriter.Workbook(bookurl)
  75.     worksheet = workbook1.add_worksheet()
  76.     title = [u'IP地址',u'CPU使用率', u'内存总量', u'内存使用率',u'根目录大小', u'根目录使用百分比', u'home大小' \
  77.         , u'home使用率', u'data使用率', u'flow流量', u'flow1流量']
  78.     format = workbook1.add_format()
  79.     worksheet.set_column(0, 15, 20)
  80.     format.set_bold()
  81.     worksheet.write_row('A1', title, format)
  82.     row = 1

  83.     for a in lists:
  84.         worksheet.write(row, 0, a["ip"])
  85.         worksheet.write(row, 1, a["cpuuse"])
  86.         worksheet.write(row, 2, a["momeryall"])
  87.         worksheet.write(row, 3, a["momeryuse"])
  88.         worksheet.write(row, 4, a["diskall"])
  89.         worksheet.write(row, 5, a["diskuse"])
  90.         worksheet.write(row, 6, a["homeall"])
  91.         worksheet.write(row, 7, a["homeuse"])
  92.         worksheet.write(row, 8, a["datause"])
  93.         worksheet.write(row, 9, a["flowuse"])
  94.         worksheet.write(row, 10, a["flow1use"])

  95.         row = row + 1
  96.     workbook1.close()


  97. def readinfo():
  98.     myfile = open('/home/system_check/xunjian/ip.txt', 'r')
  99.     listall = []
  100.     for line in myfile:
  101.         # con=line.split()
  102.         ip = line.strip()
  103.         tmplist = sshexeccmd(ip)
  104.         listall.append(tmplist)
  105.     return listall


  106. listall = readinfo()
  107. trywexrestr(listall)

ip.txt

每个ip一行

python巡检服务器自动生成excel报告



运行脚本效果如下:


python巡检服务器自动生成excel报告
阅读(2402) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~