python运维01-获取系统基础信息
1.获取系统主机名,IP,MAC地址
import socket
import uuid macs = uuid.UUID(int = uuid.getnode()).hex[-12:]
mac = ":".join([macs[e:e+2] for e in range(0,11,2)]) myname = socket.getfqdn(socket.gethostname())
myaddr = socket.gethostbyname(myname) print mac,myname,myaddr
2.获取CPU信息
#!/usr/bin/env python
# coding:utf-8 import psutil
print psutil.cpu_count() #逻辑cpu个数
print psutil.cpu_count(logical=False) #物理cpu个数
print psutil.cpu_percent() #cpu综合使用率
#是否(True/False)以列表形式列出单个cpu的使用率,interval表示统计的时间间隔
print psutil.cpu_percent(interval=1,percpu=True)
print psutil.cpu_stats()
print psutil.cpu_times()
print psutil.cpu_times_percent()
1
1
0.0
[0.0]
scpustats(ctx_switches=2163596, interrupts=4943830, soft_interrupts=4363493, syscalls=0)
scputimes(user=152.39, nice=83.79, system=160.09, idle=14650.07, iowait=431.54, irq=0.0, softirq=146.8, steal=0.0, guest=0.0, guest_nice=0.0)
scputimes(user=0.0, nice=0.0, system=0.0, idle=100.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
3.获取内存信息
#!/usr/bin/env python
# coding:utf-8 import psutil
mem = psutil.virtual_memory() #内存信息
swap = psutil.swap_memory() #分区信息 print mem
print swap unit = 1024*1024
print mem.total
print "总内存数: %s MB" % (mem.total/unit)
print "已用内存数: %s MB" % (mem.used/unit)
print "空闲内存数: %s MB" % (mem.free/unit)
print "内存使用率: %s %%" % (mem.percent)
svmem(total=1040871424, available=378040320, percent=63.7, used=507797504, free=85389312, active=218533888, inactive=597471232, buffers=0, cached=447684608, shared=3559424)
sswap(total=2147479552, used=211124224, free=1936355328, percent=9.8, sin=39768064, sout=365432832)
1040871424
总内存数: 992 MB
已用内存数: 484 MB
空闲内存数: 81 MB
内存使用率: 63.7 %
4.磁盘信息
write_bytes:IO写字节数
#!/usr/bin/env python
# coding:utf-8 import psutil
print psutil.disk_partitions() #获取磁盘分区信息
print psutil.disk_io_counters() #获取磁盘总的IO个数,读写信息
print psutil.disk_io_counters(perdisk=True) #"perdisk=True"获取单个分区的IO个数,读写信息
#获取单个分区的使用情况
print psutil.disk_usage('/')
print psutil.disk_usage('/home')
print psutil.disk_usage('/boot')
#获取磁盘分区信息
[sdiskpart(device='/dev/mapper/cl-root', mountpoint='/', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota'), sdiskpart(device='/dev/mapper/cl-home', mountpoint='/home', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota')]
#获取磁盘总的IO个数,读写信息
sdiskio(read_count=256660, write_count=134297, read_bytes=13238923264, write_bytes=4005574656, read_time=20557551, write_time=33302683, read_merged_count=9017, write_merged_count=95697, busy_time=1333693)
"perdisk=True"获取单个分区的IO个数,读写信息,以字典形式列出
{'sr0': sdiskio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0, read_merged_count=0, write_merged_count=0, busy_time=0),
'sda2': sdiskio(read_count=123678, write_count=19237, read_bytes=6608251392, write_bytes=2001718272, read_time=10145957, write_time=2467917, read_merged_count=9013, write_merged_count=95697, busy_time=638950), 'sda1': sdiskio(read_count=350, write_count=16, read_bytes=22985728, write_bytes=2138112, read_time=1993, write_time=245, read_merged_count=4, write_merged_count=0, busy_time=1991),
'dm-2': sdiskio(read_count=422, write_count=10, read_bytes=1427456, write_bytes=2117632, read_time=321, write_time=23, read_merged_count=0, write_merged_count=0, busy_time=339),
'dm-0': sdiskio(read_count=122235, write_count=25817, read_bytes=6564831744, write_bytes=1634167808, read_time=10227519, write_time=3736611, read_merged_count=0, write_merged_count=0, busy_time=640372),
'dm-1': sdiskio(read_count=9975, write_count=89217, read_bytes=41426944, write_bytes=365432832, read_time=181761, write_time=27097887, read_merged_count=0, write_merged_count=0, busy_time=52041)}
#获取单个分区的使用情况
sdiskusage(total=43985149952, used=6855770112, free=37129379840, percent=15.6)
sdiskusage(total=21472735232, used=43532288, free=21429202944, percent=0.2)
sdiskusage(total=1063256064, used=181338112, free=881917952, percent=17.1)
5.获取网络信息
#获取网络总的IO信息,默认pernic=False
In [12]: psutil.net_io_counters()
Out[12]: snetio(bytes_sent=10153696486, bytes_recv=9501486415, packets_sent=81448497, packets_recv=94917339, errin=0, errout=0, dropin=0, dropout=0) #获取每个网络接口的IO信息
In [13]: psutil.net_io_counters(pernic=True)
Out[13]:
{'eth0': snetio(bytes_sent=4565743301, bytes_recv=3913547648, packets_sent=6402022, packets_recv=19871113, errin=0, errout=0, dropin=0, dropout=0),
'lo': snetio(bytes_sent=5588080423, bytes_recv=5588080423, packets_sent=75048149, packets_recv=75048149, errin=0, errout=0, dropin=0, dropout=0)}
In [11]: psutil.net_if_addrs()
Out[11]:
{'eth0': [snic(family=<AddressFamily.AF_INET: 2>, address='192.168.0.116', netmask='255.255.255.0', broadcast='192.168.0.255', ptp=None),
snic(family=<AddressFamily.AF_INET6: 10>, address='fe80::f68e:38ff:fe85:febf%eth0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None),
snic(family=<AddressFamily.AF_PACKET: 17>, address='f4:8e:38:85:fe:bf', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)],
'lo': [snic(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None),
snic(family=<AddressFamily.AF_INET6: 10>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None),
snic(family=<AddressFamily.AF_PACKET: 17>, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp
#!/usr/bin/env python3
# coding:utf-8
import psutil
netinfo = psutil.net_if_addrs() for NetName,Info in netinfo.items():
if len(Info) < 3:
pass
elif NetName == "lo":
pass
else:
Netname = NetName
ipaddr = Info[0].address
ipMAC = Info[2].address
print(NetName,ipaddr,ipMAC)
print("--------------------------")
[root@node10 ~]# python testnet.py
('enp0s3', '192.168.0.117', '08:00:27:73:fc:5b')
--------------------------
获取网卡状态信息:
In [2]: psutil.net_if_stats()
Out[2]:
{'enp0s3': snicstats(isup=True, duplex=2, speed=1000, mtu=1500),
'lo': snicstats(isup=True, duplex=0, speed=0, mtu=65536),
'virbr0': snicstats(isup=True, duplex=0, speed=0, mtu=1500),
'virbr0-nic': snicstats(isup=False, duplex=2, speed=10, mtu=1500)}
In [4]: psutil.net_connections()
Out[4]:
[sconn(fd=15, family=2, type=1, laddr=('127.0.0.1', 39920), raddr=(), status='LISTEN', pid=7662),
sconn(fd=48, family=2, type=1, laddr=('127.0.0.1', 35170), raddr=('127.0.0.1', 58842), status='ESTABLISHED', pid=2626),
sconn(fd=3, family=2, type=1, laddr=('192.168.0.117', 22), raddr=('192.168.0.110', 58002), status='ESTABLISHED', pid=5403)]
6.其他信息
In [5]: psutil.users()
Out[5]:
[suser(name='root', terminal=':0', host='localhost', started=1506392704.0),
suser(name='root', terminal='pts/0', host='localhost', started=1506392704.0),
suser(name='root', terminal='pts/1', host='desktop-8356', started=1506409216.0)]
7.获取类似passwd文件的用户信息
In [23]: psutil.pwd.getpwall()
Out[23]:
[pwd.struct_passwd(pw_name='root', pw_passwd='x', pw_uid=0, pw_gid=0, pw_gecos='root', pw_dir='/root', pw_shell='/bin/bash'),
pwd.struct_passwd(pw_name='bin', pw_passwd='x', pw_uid=1, pw_gid=1, pw_gecos='bin', pw_dir='/bin', pw_shell='/sbin/nologin'),
pwd.struct_passwd(pw_name='daemon', pw_passwd='x', pw_uid=2, pw_gid=2, pw_gecos='daemon', pw_dir='/sbin', pw_shell='/sbin/nologin')]
python运维01-获取系统基础信息的更多相关文章
- 运维 07 Linux系统基础优化及常用命令
Linux系统基础优化及常用命令 Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令 ...
- Python运维开发基础04-语法基础【转】
上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 仅用列表+循环实现“简单的购物车程 ...
- Python运维开发基础03-语法基础 【转】
上节作业回顾(讲解+温习60分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen #只用变量和字符串+循环实现“用户登陆 ...
- Python运维开发基础02-语法基础【转】
上节作业回顾(讲解+温习60分钟) #!/bin/bash #user login User="yunjisuan" Passwd="666666" User2 ...
- Python运维开发基础09-函数基础【转】
上节作业回顾 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 实现简单的shell命令sed的替换功能 import ...
- Python运维开发基础08-文件基础【转】
一,文件的其他打开模式 "+"表示可以同时读写某个文件: r+,可读写文件(可读:可写:可追加) w+,写读(不常用) a+,同a(不常用 "U"表示在读取时, ...
- Python运维开发基础07-文件基础【转】
一,文件的基础操作 对文件操作的流程 [x] :打开文件,得到文件句柄并赋值给一个变量 [x] :通过句柄对文件进行操作 [x] :关闭文件 创建初始操作模板文件 [root@localhost sc ...
- Python运维开发基础06-语法基础【转】
上节作业回顾 (讲解+温习120分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 添加商家入口和用户入口并实现物 ...
- Python运维开发基础05-语法基础【转】
上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python # -*- coding:utf-8 -*- # author:Mr.chen import os,time Tag = ...
随机推荐
- 51nod 1183 编辑距离
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183. 题意不再赘述. 分析:大概和LCS差不多的吧 但是我用LCS ...
- thymeleaf和easyui配合可能出现的错误
thymeleaf和easyui 在easyui的内页,不再使用th:href引入静态资源文件. 在easyui页面中,script执行easyui自己的方法要加入: <script th:in ...
- HDU 2955 Robberies(0-1背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意:一个抢劫犯要去抢劫银行,给出了几家银行的资金和被抓概率,要求在被抓概率不大于给出的被抓概率的情况下, ...
- hdu 1286 找新朋友 欧拉函数模版题
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- ubuntu 14.04 安装 gflags
1.下载 git clone https://github.com/gflags/gflags 2.编译 进入源码目录(即gflags文件夹) cmake . make -j 24 sudo make ...
- JS身份证验证
window.checkIdcard = function (idcard) { var errors = new Array( "yes", "请检查输入的证件号码是否 ...
- 一步一步实现JS拖拽插件
js拖拽是常见的网页效果,本文将从零开始实现一个简单的js插件. 一.js拖拽插件的原理 常见的拖拽操作是什么样的呢?整过过程大概有下面几个步骤: 1.用鼠标点击被拖拽的元素 2.按住鼠标不放,移动鼠 ...
- _proto_和prototype区别
推荐一篇阅读:http://cometosay.com/2016/08/31/js-proto.html es中创建对象的方法 (1)对象字面量的方式 (2)new 的方式 (3)ES5中的`Obje ...
- English trip -- VC(情景课) 6 A Time
Words eleven 十一 twelve 十二 thirteen 十三 fourteen fifteen sixteen seventeen eighteen nineteen twenty ...
- LeetCode--191--位1的个数
问题描述: 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 00000 ...