使用python实现的几个运维脚本

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

#yum -y install python-pip
# yum install libffi-devel openssl-devel
#使用python启动一个web服务器
#如果是python 2.x的系列里面 这样启动一个web服务器:
# python -m SimpleHTTPServer 8080

#如果是Python 3,用下面的语句也可以启动一个http服务:
# python3 -m http.server 8000 



import os
import socket
import requests
import sys
import json
import time
import psutil


def sysInfo():

    '''
    利用psutil模块获取系统的各种统计信息
    '''
    print_str = ""

    memory_convent = 1024 * 1024
    mem =psutil.virtual_memory()

    print_str += " 内存状态如下:\n"
    print_str = print_str + " 系统的内存容量为: "+str( mem.total/( memory_convent ) ) + " MB\n"
    print_str = print_str + " 系统的内存以使用容量为: "+str( mem.used/( memory_convent ) ) + " MB\n"
    print_str = print_str + " 系统可用的内存容量为: "+str( mem.total/( memory_convent ) - mem.used/( 1024*1024 )) + "MB\n"
    print_str = print_str + " 内存的buffer容量为: "+str( mem.buffers/( memory_convent ) ) + " MB\n"
    print_str = print_str + " 内存的cache容量为:" +str( mem.cached/( memory_convent ) ) + " MB\n"

    cpu_status = psutil.cpu_times()

    print_str += " CPU状态如下:\n"
    print_str = print_str + " user = " + str( cpu_status.user ) + "\n"
    print_str = print_str + " nice = " + str( cpu_status.nice ) + "\n"
    print_str = print_str + " system = " + str( cpu_status.system ) + "\n"
    print_str = print_str + " idle = " + str ( cpu_status.idle ) + "\n"
    print_str = print_str + " iowait = " + str ( cpu_status.iowait ) + "\n"
    print_str = print_str + " irq = " + str( cpu_status.irq ) + "\n"
    print_str = print_str + " softirq = " + str ( cpu_status.softirq ) + "\n"
    print_str = print_str + " steal = " + str ( cpu_status.steal ) + "\n"
    print_str = print_str + " guest = " + str ( cpu_status.guest ) + "\n"

    print_str += " 硬盘信息如下:\n"
    disk_status = psutil.disk_partitions()
    for item in disk_status :
        print_str = print_str + " "+ str( item ) + "\n"





def diskStatus():
    '''
    监控磁盘使用情况
    '''
    new_time = time.strftime("%Y-%m-%d")
    disk_status = os.popen("df -h").readlines()

    str1 = ''.join(disk_status)
    f = open(new_time+'.log','w')
    f.write(str1)
    f.flush()
    f.close()

def sendDing(msg):
    '''
    发送钉钉消息功能
    '''
    dingding_url = 'https://oapi.dingtalk.com/robot/send?access_token=****'
    data = {"msgtype": "text","text": {"content": "内部群:"+str(msg)}}
    headers = {'Content-Type':'application/json;charset=UTF-8'}
    send_data = json.dumps(data).encode('utf-8')
    ret = requests.post(url=dingding_url,data=send_data,headers=headers)
    print(ret.text)


def chekPort():
    '''
    批量检查指定ip,端口是否开通

    '''
    sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sk.settimeout(3)
    for ip in [150,200]:
        try:
            sk.connect(("192.168.1."+str(ip),80))
            s = "192.168.1.{}:{} server open \n".format(str(ip),80)
            print(s)
        except Exception as e:
            s = "192.168.1.{}:{} server open \n".format(str(ip),80)
            print(s)
    sk.close()


def getRss():

    '''
    用脚本计算一下所有进程所占用内存大小的和
    使用ps aux 列出所有进程,过滤出RSS那列,然后求和
    '''
    list = []
    sum = 0   
    str1 = os.popen('ps aux','r').readlines()
    print(str1[0])
    for i in str1:
        str2 = i.split()
        new_rss = str2[5]
        list.append(new_rss)
    
    for i in  list[1:-1]: 
        num = int(i)
        sum = sum + num 

    print (list[0],sum)

 

posted @ 2021-10-20 16:05  凉城  阅读(983)  评论(0编辑  收藏  举报