程序员的资源宝库

网站首页 > gitee 正文

day01-系统基础信息模块(信息系统介绍)

sanyeah 2024-04-01 11:27:05 gitee 6 ℃ 0 评论

前言:2015年,由于大规模采用云平台虚拟机部署应用,每套应用系统的MySQl、接口机、WEB服务器、文件服务器需要经常进行版本升级、配置更新、安全补丁加固、iptables调整、配置核实,大量的手工操作比较繁琐,需要考虑自动化运维手段,而Python是个比较好的选择,在当当网上看到一本《Python自动化运维 技术与最佳实践》,OK,下单买回学习。

不会开发的运维,竞争力会显得更加单薄,尽早步入运维开发是必然选择。

一、系统信息性能管理psutil模块:

1.psutil是一个跨平台库(http://pythonhosted.org/psutil/)能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要用来做系统监控,性能分析,进程管理。它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系统. 

import psutil
print(psutil.cpu_times())
#scputimes(user=650613.02, nice=22.14, system=154916.5, idle=16702285.26, iowait=68894.55, irq=3.38, softirq=7075.65, steal=0.0, guest=0.0)
print(psutil.cpu_count())
print(psutil.cpu_count(logical=False))
#获取CPU使用率
print(psutil.cpu_percent())

#内存信息
mem = psutil.virtual_memory()
#svmem(total=4018601984, available=1066205184, percent=73.5, used=3904004096, free=114597888, active=3302174720, inactive=426078208, buffers=156520448, cached=795086848)
#查看内存大小
print(mem.total/1024/1024/1024)
#向上取整
print(round((mem.total/1024/1024/1024)))
#计算空闲内存
print(mem.free/mem.total)
print('%.2f%%' % (mem.free/mem.total * 100))
#计算可用内存
print('%.2f%%' % (mem.available/mem.total * 100))

#获取磁盘分区信息
print(psutil.disk_partitions())
print(psutil.disk_usage('\\'))
print(psutil.disk_usage('/'))

继续完善,批处理结果后系统输出,注意这是取单机信息,并非用于CMDB管理

物理CPU个数: 2
cup使用率: 37.5%
物理内存: 11.9 G
剩余物理内存: 7.13 G
物理内存使用率: 40 %
系统启动时间: 2015-09-17 17:52:05
当前有1个用户,分别是 fox
网卡接收流量 221.24 Mb 网卡发送流量 216.70 Mb
-----------------------------磁盘信息------------------------------------------------------------------进程信息-------------------------------------

二、IP地址管理模块IPY 

       IPy模块提供了专门针对IPV4地址与IPV6地址的类与工具,可以帮助实现计算子网掩码、网络掩码、广播地址、子网数、IP类型等

from IPy import IP
ip_s = input('Please input an IP or net-range: ')
ips = IP(ip_s)
if len(ips) > 1:
    print('net: %s' % ips.net())
    print('netmask: %s' % ips.netmask())
    print('broadcast: %s' % ips.broadcast())
    print('reverse address: %s' % ips.reverseNames()[0])
    print('subnet: %s' % len(ips))
else:
    print('reverse address: %s' % ips.reverseNames()[0])
print('hexadecimal: %s' % ips.strHex())
print('binary ip: %s' % ips.strBin())
print('iptype: %s' % ips.iptype())
输入  Please input an IP or net-range: 192.168.80.0/24
输出如下
net: 192.168.80.0
netmask: 255.255.255.0
broadcast: 192.168.80.255
reverse address: 80.168.192.in-addr.arpa.
subnet: 256
hexadecimal: 0xc0a85000
binary ip: 11000000101010000101000000000000
iptype: PRIVATE
注,网络维护人员很熟配置,IP规划可能用不上这个工具

三、DNS处理模块dnspython  

简介:dnspython – 是python实现的一个DNS工具包,利用其查询功能来实现dns的服务监控及解析结果的校验

安装dnspython    pip install dnspython

 

 

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表