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 = ...
随机推荐
- Pairs Forming LCM (LCM+ 唯一分解定理)题解
Pairs Forming LCM Find the result of the following code: ; i <= n; i++ ) for( int j = i; j ...
- 使用CLR Profiler查看C#运行程序的内存占用情况
http://blog.csdn.net/wy3552128/article/details/8158938 https://msdn.microsoft.com/en-us/library/ff65 ...
- 【第二十九章】 springboot + zipkin + mysql
zipkin的数据存储可以存在4个地方: 内存(仅用于测试,数据不会持久化,zipkin-server关掉,数据就没有了) 这也是之前使用的 mysql 可能是最熟悉的方式 es Cassandra ...
- UVa 1610 聚会游戏
https://vjudge.net/problem/UVA-1610 题意:输入一个n个字符串的集合D,找一个长度最短的字符串S,使得D中恰好有一半串小于等于S,另一半串大于S. 思路:先拍序,然后 ...
- zeptojs库解读3之ajax模块
对于ajax,三步骤,第一,创建xhr对象:第二,发送请求:第三,处理响应. 但在编写过程中,实际中会碰到以下问题, 1.超时 2.跨域 3.后退 解决方法: 1.超时 设置定时器,规定的时间内未返回 ...
- DDD领域模型和充血对象
DDD领域模型 官方说法 领域驱动设计,它是对面向对象的的分析和设计(OOAD,Object Orient Analysis Design)的一个补充,对技术框架进行了分层规划,同时对每个类进行了策略 ...
- Unity搭建简单的图片服务器
具体要实现的目标是:将图片手动拷贝到服务器,然后在Unity中点击按钮将服务器中的图片加载到Unity中. 首先简答解释下 WAMP(Windows + Apache + Mysql + PHP),一 ...
- Vue.js教程--基础(实例 模版语法template computed, watch v-if, v-show v-for, 一个组件的v-for.)
官网:https://cn.vuejs.org/v2/guide/index.html Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统. 视频教程:https: ...
- highcharts图表配置参数汇总
一.chart的部分相关属性说明 renderTo: 'container', //图表的页面显示容器(也就是要显示到的div) chart.events.addSeries:添加数列 ...
- Kali Linux on Android # 实测:小米2s离线安装Kali Linux
小米2s 离线安装Kali Linux 2017年2月4日 by 小甘丶 前段时间也研究过一两天,没弄明白,今天突然来兴致了,说研究一下吧,结果一不小心,就弄明白了! 第一次研究,主要是没弄明白这个 ...