Python运维-获取当前操作系统的各种信息
#通过Python的psutil模块,获取当前系统的各种信息(比如内存,cpu,磁盘,登录用户等),并将信息进行备份
# coding=utf-8 # 获取系统基本信息
import sys
import psutil
import time
import os #获取当前时间
time_str = time.strftime( "%Y-%m-%d", time.localtime( ) )
file_name = "./" + time_str + ".log" if os.path.exists ( file_name ) == False :
os.mknod( file_name )
handle = open ( file_name , "w" )
else :
handle = open ( file_name , "a" ) #获取命令行参数的个数
if len( sys.argv ) == 1 :
print_type = 1
else :
print_type = 2 def isset ( list_arr , name ) :
if name in list_arr :
return True
else :
return False print_str = ""; #获取系统内存使用情况
if ( print_type == 1 ) or isset( sys.argv,"mem" ) :
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的相关信息
if ( print_type == 1 ) or isset( sys.argv,"cpu" ) :
print_str += " CPU状态如下:\n"
cpu_status = psutil.cpu_times()
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" #查看硬盘基本信息
if ( print_type == 1 ) or isset ( sys.argv,"disk" ) :
print_str += " 硬盘信息如下:\n"
disk_status = psutil.disk_partitions()
for item in disk_status :
print_str = print_str + " "+ str( item ) + "\n" #查看当前登录的用户信息
if ( print_type == 1 ) or isset ( sys.argv,"user" ) :
print_str += " 登录用户信息如下:\n "
user_status = psutil.users()
for item in user_status :
print_str = print_str + " "+ str( item ) + "\n" print_str += "---------------------------------------------------------------\n"
print ( print_str )
handle.write( print_str )
handle.close()
Python运维-获取当前操作系统的各种信息的更多相关文章
- python运维01-获取系统基础信息
1.获取系统主机名,IP,MAC地址 import socket import uuid macs = uuid.UUID(int = uuid.getnode()).hex[-12:] mac = ...
- Python 运维
1.python解释器提供提供的小工具 1.1 一秒钟启动一个下载服务器 进入要下载文件的目录(shift+鼠标右键可以很快的在当前目录打开一个cmd) python2: python2 -m Sim ...
- Python运维开发基础04-语法基础【转】
上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 仅用列表+循环实现“简单的购物车程 ...
- Python运维开发基础09-函数基础【转】
上节作业回顾 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 实现简单的shell命令sed的替换功能 import ...
- Python运维开发基础07-文件基础【转】
一,文件的基础操作 对文件操作的流程 [x] :打开文件,得到文件句柄并赋值给一个变量 [x] :通过句柄对文件进行操作 [x] :关闭文件 创建初始操作模板文件 [root@localhost sc ...
- Python运维开发基础05-语法基础【转】
上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python # -*- coding:utf-8 -*- # author:Mr.chen import os,time Tag = ...
- 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运维6期 不加密
老男孩-python运维6期 不加密,连夜整理出来分享给大家老男孩的python教程确实不错. 教程目录: 下载地址:http://www.fu83.cn/thread-204-1-1.html
随机推荐
- Bentley二次开发中的,沿曲线构造拉伸实体问题
引用文件:Bentley.Interop.MicroStationDGN 本人开发过程中遇到问题: 创建多个线段及弧线,通过自动创建复杂链获得,沿曲线构造拉伸实体的Path参数,拉伸曲线路径首尾特别近 ...
- docker-bridge network
根据下面这图示,可以了解两个container之间是如何通信的 通过ip link 和 ip a可以看到本地有个docker0 link. [root@localhost ~]# ip link : ...
- 第五节 RabbitMQ在C#端的应用-消息收发
原文:第五节 RabbitMQ在C#端的应用-消息收发 版权声明:未经本人同意,不得转载该文章,谢谢 https://blog.csdn.net/phocus1/article/details/873 ...
- 循环结构select 举例
- codelite配置信息
codelite下编译执行wxwidgets库需要修改链接库如下: 原来的c++ compiler配置-g;-O0;-Wall;$(shell wx-config --cflags --debug) ...
- 巨好看的xshell配色
推荐字体Lucida console [FlatUI] text=e5e5e5 cyan(bold)=16a085 text(bold)=ecf0f1 magenta=9b59b6 green=2ec ...
- java--ArrayList,LinkedList应用比较
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class ListDem ...
- Git分支,合并,切换分支的使用
1.创建合并分支 在我们每次的提交,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支.HEAD指针严格来说不是指向提交 ...
- PHP发送公众号模板消息
<?php /* * 模板消息发送,电脑端测试时需要手动填写openid * 微信端会自动获取当前openid发送无需填写 */ header("Content-type: text/ ...
- 【Flutter学习】页面布局之基础布局组件
一,概述 Flutter中拥有30多种预定义的布局widget,常用的有Container.Padding.Center.Flex.Row.Colum.ListView.GridView.按照< ...