Python日常运维脚本
1 扫描Windows系统CPU、内存、磁盘利用率
1.1 实现代码
#!usr/bin/env python
#-*- coding:utf-8 _*- """
@author:T-guoh
@file: server_monitor.py
@time: 2019/03/18
""" import psutil
import os
import socket cpu = {'user':0,'system':0,'idle':0,'percent':0}
mem = {'total' : 0, 'avaiable' : 0, 'percent' : 0, 'used' : 0, 'free' : 0}
disk_id = []
disk_total = []
disk_used = []
disk_free = []
disk_percent = [] #获取CPU信息
def get_cpu_info():
cpu_times = psutil.cpu_times()
cpu['user'] = cpu_times.user
cpu['system'] = cpu_times.system
cpu['idle'] = cpu_times.idle
cpu['percent'] = psutil.cpu_percent(interval=2) #获取Memory信息
def get_mem_info():
mem_info = psutil.virtual_memory()
mem['total'] = mem_info.total
mem['avaiable'] = mem_info.available
mem['percent'] = mem_info.percent
mem['used'] = mem_info.percent
mem['free'] = mem_info.free
#获取Disk信息
def get_disk_info():
for id in psutil.disk_partitions():
if 'cdrom' in id.opts or id.fstype == '':
continue
disk_name = id.device.split(':')
s = disk_name[0]
disk_id.append(s)
disk_info = psutil.disk_usage(id.device)
disk_total.append(disk_info.total)
disk_used.append(disk_info.used)
disk_percent.append(disk_info.percent)
disk_free.append(disk_info.free)
#定义主函数
def main():
get_cpu_info()
cpu_status = cpu['percent']
get_mem_info()
mem_status = mem['percent']
hostname = os.environ['COMPUTERNAME']
username = os.environ['USERNAME']
ip_addr = socket.gethostbyname(hostname)
print('======基本信息======')
print('主机名:'+ hostname+ '\nIP地址:' + ip_addr + '\n用户名:' + username)
print('====================')
print('\n=====资源使用率=====')
print('CPU使用率:%s %%' % cpu_status)
print('Mem使用率:%s %%' % mem_status)
get_disk_info()
for i in range(len(disk_id)):
print('%s盘空闲率:%s %%' % (disk_id[i], round(100 - disk_percent[i], 2)))
print('====================')
os.system('pause') if __name__ == '__main__':
main()
1.2 运行结果

2 扫描Windows系统Top目录与文件大小
2.1 实现代码
#!usr/bin/env python
#-*- coding:utf-8 _*- """
@author:T-guoh
@file: server_monitor.py
@time: 2019/03/18
""" import os #统计指定目录下各个文件夹的大小,方便观察具体目录的大小
def CountDirSize(srcDir):
dirSizeDict = {}
fileSizeDict = {}
for fileList in os.listdir(srcDir):
filePath = os.path.join(srcDir,fileList)
if os.path.isdir(filePath):
size = 0
for roots,dirs,files in os.walk(filePath):
for fileName in files:
fileNamePath = os.path.join(roots,fileName)
sizetemp = 0
try:
sizetemp = os.path.getsize(fileNamePath)
if sizetemp > 1024*1024:
fileSizeDict[fileNamePath] = sizetemp
except:
sizetemp = 0
size += sizetemp
dirSizeDict[fileList] = size
#按照从大到小的顺序降序排列
dirSizeDict = sorted(dirSizeDict.items(),key = lambda dirSizeDict:dirSizeDict[1],reverse = True)
Sum = 0
#各个目录占用空间
print("Each directory takes up space:")
for ele in dirSizeDict:
temp = ele[1]/1024/1024
Sum += temp
#以单位为M,输出大小
print(ele[0] + ":" + str(temp) + " M")
#以单位为G,输出大小
print("Total use:" + str(Sum / 1024) +" G")
print("")
#前20个大文件占用空间
print("Top 20 large file takes up space:")
fileSizeDict = sorted(fileSizeDict.items(),key = lambda fileSizeDict:fileSizeDict[1],reverse = True)
for ele in fileSizeDict[:20]:
print(ele[0] + ":" +str(ele[1]/1024/1024) + " M") if __name__ == "__main__":
srcDir = "c:\\"
CountDirSize(srcDir)
2.2 运行结果

Python日常运维脚本的更多相关文章
- 转:python常用运维脚本实例
python常用运维脚本实例 转载 file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函 ...
- python常用运维脚本实例【转】
file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函数open()来打开一个文件 . 首先 ...
- python常用运维脚本实例
转载 file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函数open()来打开一个文件 ...
- Python自动化运维工具fabric的安装
使用shell命令进行复杂的运维时,代码往往变得复杂难懂,而使用python脚本语言来编写运维程序,就相当于开发普通的应用一样,所以维护和扩展都比较简单,更重要的是python运维工具fabric能自 ...
- Jenkins日常运维笔记-重启数据覆盖问题、迁移、基于java代码发版(maven构建)
之前在公司机房部署了一套jenkins环境,现需要迁移至IDC机房服务器上,迁移过程中记录了一些细节:1)jenkins默认的主目录放在当前用户家目录路径下的.jenkins目录中.如jenkins使 ...
- Python自动化运维工具-Fabric部署及使用总结
使用shell命令进行复杂的运维时,代码往往变得复杂难懂,而使用python脚本语言来编写运维程序,就相当于开发普通的应用一样,所以维护和扩展都比较简单,更重要的是python运维工具fabric能自 ...
- linux日常运维工作
Linux的使用环境也日趋成熟,各种开源产品络绎不绝,大有百花齐放的盛景,那么当Linux落地企业,回归工作时,我们还要面对这Linux运维方面的诸多问题,今天我们特意组织一场有关Linux 在企业运 ...
- hadoop日常运维与升级总结
日常运维 升级 问题处理方法 日常运维 进程管理 由于配置文件的更改,需要重启生效, 或者是进程自己因某种致命原因终止, 或者发现进程工作出现异常等情况下,需要进行手动进程的关闭或启动, 或者是增删节 ...
- zookeeper 用法和日常运维
本文以ZooKeeper3.4.3版本的官方指南为基础:http://zookeeper.apache.org/doc/r3.4.3/zookeeperAdmin.html,补充一些作者运维实践中的要 ...
随机推荐
- 【Android】6.0 添加Menu菜单组件、Intent启动活动、显式Intent、隐式Intent
1.0 在helloworld项目基础上创建活动SecondActivity: 2.0 其中main.xml: <?xml version="1.0" encoding=&q ...
- webstrom 在脚本区域写其他语言得到语法提示
webstrom 在脚本区域写其他语言得到语法提示 webstrom 的提示小灯泡 点击inject language or reference 选择相应的语言. 如果写的内容比较多, 可以按照web ...
- sql产生流水号
一个产生流水号(年月日+5位流水号)的存储过程 现在客户有一个需求,要产生一个流水号,如090611+000001(年月日+五位流水号),此流水号在数据库表中是主键,且为varchar类 型.如果在当 ...
- 单例模式(C++)
#include <iostream> #include <string> using namespace std; class singleton { private: si ...
- db2的count()函数和sum()函数的用法
一.count()函数可以使用参数,例如count(*)和count(列名) count(*)用来计算在指定条件下,满足条件的行数,例如: select count(*) from tablename ...
- SQL Server 数据库修改后不允许保存
打开 工具 - > 选项 -> 设计器,确认[阻止保存要求重新创建表的更改]项是否选中,如果选中,取消即可.
- JavaMelody监控spring、struts
前言 前面讲过了Javamelody的基本配置,如何使用Javamelody来监控JDBC以及SQL. 这里继续讲解一下如何监控struts和spring. 手码不易,转载请注明:xingoo 由于s ...
- Ubuntu adb device
在ubuntu上经常出现系统无法识别android手机的情况,要解决这个问题可以用以下方法: 1. 执行 "android update adb", 这会创建"~/.an ...
- python 用cookie模拟登陆网站
import re import requests def get_info(url): headers = { "Cookie" :"***************** ...
- GreenPlum 与hadoop什么关系?(转)
没关系. gp 可以处理大量数据, hadoop 可以处理海量. gp 只能处理湖量,或者河量. 无法处理海量. 作者:SallyLeo链接:https://www.zhihu.com/questio ...