python运维常用脚本

本文介绍了几个Python运维脚本,包括判断目录、系统内存和磁盘检测、统计Nginx日志中IP访问量、查看网段IP、GitLab钩子自动化操作、DNS解析和Redis缓存清除。这些脚本涵盖了日常运维中的关键任务,有助于提升效率。
摘要由CSDN通过智能技术生成

判断是否是一个目录

#!/usr/bin/env python3

-- coding: utf-8 --

@Time : 2018-12-18 15:16

@Author : opsonly

@Site :

@File : opsUse.py

@Software: PyCharm

import os
dir = “/var/www/html/EnjoyCarApi/”
if os.path.isdir(dir):
print(’%s is a dir’ % dir)
else:
print(’%s is not a dir’ % dir)
系统内存与磁盘检测

#!/usr/bin/env python3

-- coding: utf-8 --

@Time : 2018-12-17 17:16

@Author : opsonly

@Site :

@File : systemissue.py

@Software: PyCharm

import psutil
def memissue():
print(‘内存信息:’)
mem = psutil.virtual_memory()

单位换算为MB

memtotal = mem.total/1024/1024
memused = mem.used/1024/1024
membaifen = str(mem.used/mem.total100) + ‘%’
print(’%.2fMB’ % memused)
print(’%.2fMB’ % memtotal)
print(membaifen)
def cuplist():
print(‘磁盘信息:’)
disk = psutil.disk_partitions()
diskuse = psutil.disk_usage(’/’)
#单位换算为GB
diskused = diskuse.used / 1024 / 1024 / 1024
disktotal = diskuse.total / 1024 / 1024 / 1024
diskbaifen = diskused / disktotal * 100
print(’%.2fGB’ % diskused)
print(’%.2fGB’ % disktotal)
print(’%.2f’ % diskbaifen)
memissue()
print(’
******************’)
cuplist()
统计nginx日志前十ip访问量并以柱状图显示

#!/usr/bin/env python3

-- coding: utf-8 --

@Time : 2018-12-18 15:49

@Author : opsonly

@Site :

@File : nginx_ip.py

@Software: PyCharm

import matplotlib.pyplot as plt

nginx_file = ‘nginx2018-12-18_07:45:26’
ip = {}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值