Python實現(xiàn)Linux服務器自動巡檢腳本
概述
最近抽時間寫了一個自動巡檢腳本,只需配置服務器ip、用戶名、密碼即可實現(xiàn)服務器自動巡檢,巡檢日志以txt文件輸出,免去了挨個敲命令巡檢的麻煩,腳本比較簡單可拉去代碼進行二次開發(fā)??梢约由隙〞r任務和巡檢報告發(fā)送郵箱功能,實現(xiàn)完全托管。
源碼
以下是完整代碼:
#!/usr/bin/python3
from netmiko.huawei.huawei import HuaweiSSH
import os
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
CMD_STR = 'cmd_str'
CMD_NAME = 'name'
IP = 'ip'
USER_NAME = 'username'
PASSWORD = 'password'
# email config
smtp_host = '你的郵箱發(fā)送付服務地址'
smtp_port = 25
smtp_email = '你的發(fā)送郵箱號'
# 用戶名,默認為發(fā)件人郵箱前綴
smtp_user ='你的發(fā)送郵箱號'
# 密碼(注意,某些郵箱需要為SMTP服務單獨設置授權碼,詳情查看相關幫助)
smtp_password = '你的發(fā)送郵箱密碼'
class ServerMaintenance:
net_connect = None
curr_dir = os.getcwd()
fo = None
file_name = None
email_files = []
# 建立服務器連接
def Connect_HuaweiSSH(self, _ip, _user_name, _pw):
try:
S5720 = {
'device_type': 'huawei',
'ip': _ip,
'username': _user_name,
'password': _pw,
}
# 實例化HuaweiSSH
net_connect = HuaweiSSH(**S5720)
self.net_connect = net_connect
except:
print("連接" + _ip + "錯誤")
# 運行指令
def execute(self, cmd_str, cmd_name):
result = self.net_connect.send_command(cmd_str)
print(result)
self.printFile('\n-------------------------任務【' + cmd_name + '】開始巡查 -------------------------\n')
self.printFile(result)
self.printFile('\n-------------------------任務【' + cmd_name + '】結束巡查 -------------------------\n')
return result
# 關閉連接
def disconnect(self):
self.net_connect.disconnect()
print("連接斷開" + self.net_connect)
# 文件寫入
def printFile(self, txt):
self.fo.write(txt)
# 發(fā)送郵箱
def sendEmail(self):
msg = MIMEMultipart()
msg['From'] = Header("master", 'utf-8')
msg['To'] = Header('13693567027@163.com', 'utf-8')
subject = 'Test Email'
msg['Subject'] = Header(subject, 'utf-8')
# 郵件正文內(nèi)容
content = ''
for filename in self.email_files:
content+=filename+'\n'
# 郵件附件
att = MIMEText(open(filename+'.txt', 'rb').read(), 'base64', 'utf-8')
att['Content-Type'] = 'application/octet-stream' # 指定文件格式
att["Content-Disposition"] = 'attachment; filename="服務器巡檢報告.txt"'
msg.attach(att)
msg.attach(MIMEText(content, 'plain', 'utf-8'))
try:
# # 創(chuàng)建安全連接,使用SMTP_SSL連接SMTP服務器
smtp_client = smtplib.SMTP(smtp_host) # 實例化對象
smtp_client.connect(smtp_host, smtp_port) # 連接163郵箱服務器,端口號為25
# # 向服務器標識用戶身份
smtp_client.login(smtp_user, smtp_password)
# 發(fā)送郵件
smtp_client.sendmail(smtp_user, '13693567027@163.com', msg.as_string())
print("郵件發(fā)送成功")
except smtplib.SMTPException as e:
print("Error: 無法發(fā)送郵件 ", e)
# 關閉SMTP連接
smtp_client.quit()
def run(self,commands,hosts):
for host in hosts:
self.file_name = "服務器巡查_" + host[IP] + '_' + datetime.now().strftime(format="%Y年%m月%d日%H時%M分%S秒")
self.email_files.append(self.file_name)
self.fo = open(self.file_name + ".txt", 'w+', encoding='utf-8')
self.Connect_HuaweiSSH(host[IP], host[USER_NAME], host[PASSWORD])
self.printFile(
'\n================================主機開始巡檢: ' + host[IP] + '================================\n')
for command in commands:
self.execute(command[CMD_STR], command[CMD_NAME])
self.printFile(
'\n================================主機巡檢結束: ' + host[IP] + '================================\n')
commands = [
{CMD_STR: "ps -ef |grep java", CMD_NAME: "運行中的應用"},
{CMD_STR: "docker ps", CMD_NAME: "運行中的容器"},
{CMD_STR: "df -TH", CMD_NAME: "磁盤空間"},
{CMD_STR: "free -h", CMD_NAME: "內(nèi)存"},
{CMD_STR: "ps -ef", CMD_NAME: "進程"},
{CMD_STR: "crontab -l", CMD_NAME: "定時任務"},
{CMD_STR: "cat /var/log/messages|grep ERROR && dmesg |grep ERROR", CMD_NAME: "操作系統(tǒng)日志"},
{CMD_STR: "date", CMD_NAME: "操作系統(tǒng)時間"},
{CMD_STR: "firewall-cmd --list-all", CMD_NAME: "防火墻"},
{CMD_STR: "dmidecode -t system", CMD_NAME: "服務器型號"},
{CMD_STR: "uname -a && uname -r", CMD_NAME: "操作系統(tǒng)與內(nèi)核"},
{CMD_STR: "last", CMD_NAME: "用戶登錄日志"},
]
hosts = [
{IP: '你的服務器ip', USER_NAME: '你的服務器賬號', PASSWORD: '你的服務器密碼'}
]
serverMain = ServerMaintenance()
serverMain.run(commands,hosts)
serverMain.sendEmail()Linux系統(tǒng)巡檢常用命令
1、查看服務器型號: dmidecode grep Product Name 或者 dmidecode -t system 或者 dmidecode -t1 或者dmidecodegrep“Product"
2、查看主板的序列號: dmidecode grep Serial Number 或者 dmidecode -t system|grep Serial Number
3、統(tǒng)-查看服務器SN序列號和型號: dmidecode grep “System lnformation” -A9 egrepManufacturer/Product Serial!
4、查看內(nèi)核/操作系統(tǒng): uname-a/r
5、查看操作系統(tǒng)版本: head -n ]/etc/issue #是數(shù)字1不是字母L查看centos操作系統(tǒng)版本: cat /etc/centos-release
查看版本: cat /proc/version #類似uname -r
6、查看CPU信息 (型號): cat/proc/cpuinfolgrep namelcut-f2 -d:|unig -c
7、查看物理CPU個數(shù): cat /proc/cpuinfol grep“physicalid” sort uniql wc -l
8、查看每個物理CPU中core的個數(shù)(即核數(shù)): cat /proc/cpuinfo grep “cpu cores”|uniq
9、查看邏輯CPU的個數(shù): cat /proc/cpuinfolgrep“processor”]wc-
10、查看內(nèi)存信息: cat /proc/meminfo或者free 命令 或者cat /proc/meminfo grep MemTota查看內(nèi)存信息: dmidecode -t memory 或者dmidecode -t memory|grep
- 查看內(nèi)存總量: grep MemTotal /proc/meminfo
- 查看空閑內(nèi)存量: grep MemFree /proc/meminfo
11、查看所有swap分區(qū)的信息: cat /proc/swaps
查看內(nèi)存使用量和交換區(qū)使用量: free-m
12、查看磁盤信息: fdisk - 或者fdisk -grep Disk
查看各分區(qū)使用情況: df -h
13、列出所有啟動的系統(tǒng)服務: chkconfig - list|grep on
14、查看磁盤IO的性能:iostat -x 10
15、列出所有PCI設備: Ispci -tv
- 列出所有USB設備: lsusb -tv
- 列出加載的內(nèi)核模塊: lsmod
- 查看pci設備的信息: cat /proc/pci
16、列出所有USB設備的linux系統(tǒng)信息命令:Isusb -tv
17、查看計算機名: hostname
18、查看指定目錄的大小: du -sh< 目錄名>
19、查看系統(tǒng)運行時間、用戶數(shù)、負載: uptime
查看系統(tǒng)負載: cat /proc/loadavg
20、查看所有用戶的定時任務: crontab -
21、查看掛接的分區(qū)狀態(tài): mount|column -t
22、查看所有網(wǎng)絡接口的屬性: ifconfig
23、查看防火墻設置:iptables -L
24、查看路由表: route -n
25、查看所有監(jiān)聽端口: netstat -Intp
26、查看所有已經(jīng)建立的連接: netstat -antp
查看網(wǎng)絡統(tǒng)計信息: netstat -s
27、查看設備io端口: cat /proc/ioports
29、查看中斷: cat /proc/interrupts
30、查看環(huán)境變量:env
31、查看所有進程:ps -ef
實時顯示進程狀態(tài): top
32、查看活動用戶: who
33、查看看磁盤參數(shù)(僅適用于IDE設備): hdparm -i/dev/hda
查看啟動時IDE設備檢測狀況: dmesg|grepIDE
34、查看指定用戶信息: id< 用戶名>
35、查看用戶登錄日志: last
36、查看系統(tǒng)所有用戶: cut -d:f1 /etc/passwd
37、查看系統(tǒng)所有組: cut -d: -f1 /etc/group
38、安全檢查: cat /etc/passwd cat /etc/group
39、查看DB2數(shù)據(jù)庫的表空間詳情: db2 list tablespaces show detail
40、日志查看:
- dmesg<目錄/日志文件>
- cat /var <目錄/日志文件>
- tail -f <目錄/日志文件>/var/log/message 系統(tǒng)啟動后的信息和錯誤日志,是Red Hat Linux中最常用的日志之-/var/log/secure 與安全相關的日志信息
- /var/log/maillog 與郵件相關的日志信息
- /var/log/cron 與定時任務相關的日志信息
- /var/log/spooler 與UUCP和news設備相關的日志信息
- /var/log/boot.log 守護進程啟動和停止相關的日志消息
方法補充
Linux-Python運維自動巡檢腳本
1.使用說明
createTime: 2022-12-21
createBy: lln
createInfo:
檢查服務器磁盤、內(nèi)存、網(wǎng)絡、docker容器等信息,以json格式輸出至同目錄下的report文件夾中,便于運維人員查看。
環(huán)境說明
Centos版本 >=7
Python2版本 >=2.6 (兼容python3)
使用步驟
1、將腳本文件linuxOpsStartUp.py放入任意目錄下
2、執(zhí)行 python linuxOpsStartUp.py 命令,進行服務器信息檢查,檢查結果輸出至同目錄下report文件夾中。
檢查結果示例
[
{
"最后啟動": [
"15:08 "
],
"發(fā)行版本": [
"CentOS Linux release 7.9.2009 (Core)"
],
"當前時間": [
"2022-12-20 17:50:13"
],
"系統(tǒng)": [
"GNU/Linux"
],
"時區(qū)信息": [
"Tue, 20 Dec 2022 17:50:13 +0800"
],
"運行時間": [
"2022-12-20 17:50:13"
],
"內(nèi)核": [
"3.10.0-1160.6.1.el7.x86_64"
],
"主機名": [
"localhost.localdomain"
]
},
{
"物理CPU個數(shù)": [
"1"
],
"CPU架構": [
"x86_64"
],
"每CPU核心數(shù)": [
"4"
],
"CPU型號": [
"Intel(R) Core(TM) i5-6400 CPU @ 2.70GHz"
],
"邏輯CPU個數(shù)": [
"4"
]
},
{
"內(nèi)存總覽": [
" total used free shared buff/cache available",
"Mem: 15G 9.2G 307M 783M 5.9G 5.1G",
"Swap: 7.8G 237M 7.6G"
]
},
{
"索引總量(MB)": 1125058,
"硬盤使用量(GB)": 1060,
"磁盤總覽": [
"文件系統(tǒng) 容量 已用 可用 已用% 掛載點",
"devtmpfs 7.8G 0 7.8G 0% /dev",
"tmpfs 7.8G 0 7.8G 0% /dev/shm",
"tmpfs 7.8G 732M 7.1G 10% /run",
"tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup",
"/dev/mapper/centos-root 50G 31G 20G 62% /",
"/dev/sda2 1014M 188M 827M 19% /boot",
"/dev/sda1 200M 12M 189M 6% /boot/efi",
"/dev/mapper/centos-home 2.0T 38G 2.0T 2% /home",
"tmpfs 1.6G 0 1.6G 0% /run/user/0"
],
"硬盤總量(GB)": 3726,
"硬盤使用比例(%)": "28.46%",
"索引剩余量(MB)": 1095859,
"索引使用量(MB)": 29198,
"硬盤空余量(GB)": 2665,
"索引使用比例(%)": "2.60%"
},
{
"IP": [
"enp3s0 192.168.11.127/24,br-1849b047c9dd 172.19.0.1/16,docker0 172.17.0.1/16,br-7e3fcfcbbbdf 172.18.0.1/16,br-e9753d63540c 172.20.0.1/16"
],
"GATEWAY": [
"192.168.11.1"
],
"DNS": [
"223.5.5.5"
]
},
{
"空密碼用戶": [
"test"
],
"所有用戶名": [
"root",
"bin",
"daemon",
"ntp"
]
},
{
"jdk信息": [
"openjdk version \"1.8.0_275\"",
"OpenJDK Runtime Environment (build 1.8.0_275-b01)",
"OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)"
]
},
{
"防火墻狀態(tài)": [
"not running"
]
},
{
"ssh開啟狀態(tài)": [
"active"
],
"ssh運行情況": [
"tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1062/sshd ",
"tcp 0 0 192.168.11.127:22 192.168.11.194:50779 ESTABLISHED 10513/sshd: root@pt ",
"tcp 0 0 192.168.11.127:22 192.168.11.194:52458 ESTABLISHED 17626/sshd: root@no ",
"tcp6 0 0 :::22 :::* LISTEN 1062/sshd "
]
},
{
"ntp運行情況": [
"active"
]
},
{
"docker-compose version": [
"docker-compose version 1.29.2, build unknown"
],
"docker version": [
"Docker version 20.10.0, build 7287ab3"
],
"docket stats": [
"CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS",
"d36c48b5c621 sinfcloud-rabbitmq 1.31% 122.7MiB / 15.47GiB 0.77% 3.78GB / 4.09GB 63.2MB / 2.58MB 29",
"40db1a93ec2d linux-command 0.00% 144KiB / 15.47GiB 0.00% 62.1kB / 1.3MB 1.44MB / 0B 1"
]
}
]
腳本完整代碼
# -*- coding: utf-8 -*-
"""
linux 自動化腳本
# @Time: 2022/11/4 10:20
# @Author: lln
# @File: linuxOpsStartUp.py
"""
import json
import os
import platform
import time
def runCommand(command):
"""
執(zhí)行命令,將所有讀到的數(shù)據(jù)去除空行
:param command: 命令
:return: 去除空行后的命令
"""
lines = os.popen(command).readlines()
res = []
for line in lines:
res.append(line.replace('\n', ''))
return res
def getSystemInfo():
"""
使用內(nèi)置庫獲取系統(tǒng)信息
"""
res = {
"操作系統(tǒng)名稱及版本號": platform.platform(),
"操作系統(tǒng)版本號": platform.version(),
"操作系統(tǒng)的位數(shù)": platform.architecture(),
"計算機類型": platform.machine(),
"網(wǎng)絡名稱": platform.node(),
"處理器信息": platform.processor(),
}
return res
def getSystemStatus():
"""
系統(tǒng)信息,僅支持centos進行查詢
"""
# 系統(tǒng)
OS = runCommand("uname -o")
# 發(fā)行版本
Release = runCommand("cat /etc/redhat-release 2>/dev/null")
# 內(nèi)核
Kernel = runCommand("uname -r")
# 主機名
Hostname = runCommand("uname -n")
# 當前時間
LocalTime = runCommand("date +'%F %T'")
# 最后啟動
LastReboot = runCommand("who -b | awk '{print $3,$4}'")
# 運行時間
Uptime = runCommand("date +'%F %T'")
# 當前時區(qū)信息
time_zone = runCommand("date -R")
res = {
"系統(tǒng)": OS,
"發(fā)行版本": Release,
"內(nèi)核": Kernel,
"主機名": Hostname,
"當前時間": LocalTime,
"最后啟動": LastReboot,
"運行時間": Uptime,
"時區(qū)信息": time_zone
}
return res
def getCpuStatus():
"""
CPU信息
"""
# 物理CPU個數(shù)
physical_cpus = runCommand("grep 'physical id' /proc/cpuinfo| sort | uniq | wc -l")
# 邏輯CPU個數(shù)
virt_cpus = runCommand("grep 'processor' /proc/cpuinfo | wc -l")
# 每CPU核心數(shù)
cpu_kernels = runCommand("grep 'cores' /proc/cpuinfo|uniq| awk -F ': ' '{print $2}'")
# CPU型號
cpu_type = runCommand("grep 'model name' /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq")
# CPU架構
cpu_arch = runCommand("uname -m")
res = {
'物理CPU個數(shù)': physical_cpus,
'邏輯CPU個數(shù)': virt_cpus,
'每CPU核心數(shù)': cpu_kernels,
'CPU型號': cpu_type,
'CPU架構': cpu_arch
}
return res
def getMemStatus():
"""
內(nèi)存信息
"""
# 總內(nèi)存
MemTotal = runCommand("grep MemTotal /proc/meminfo| awk '{print $2}'")
MemTotal_Num = map(float, MemTotal)[0]
# 可用內(nèi)存
MemFree = runCommand("grep MemFree /proc/meminfo| awk '{print $2}'")
MemFree_Num = map(float, MemFree)[0]
# 比例
Proportion = '{:.4%}'.format(MemFree_Num / MemTotal_Num)
res = {
'總內(nèi)存(GB)': '{:.5}'.format(float(MemTotal_Num / 1024 / 1024)),
'可用內(nèi)存(GB)': '{:.5}'.format(float(MemFree_Num / 1024 / 1024)),
'已用比例(%)': Proportion
}
return res
def getMemStatusSimple():
MemTotal = runCommand("free -h")
res = {
'內(nèi)存總覽': MemTotal
}
return res
def getDiskStatus():
"""
磁盤檢查
"""
# 生成臨時數(shù)據(jù)記錄文件
# os.popen("df -TP | sed '1d' | awk '$2!='tmpfs'{print}'")
# os.popen("df -hTP | sed 's/Mounted on/Mounted/'> /tmp/disk")
# 硬盤總量
DiskAllInfo = runCommand("df -h | grep -v docker")
DiskTotal = runCommand("df -TP | sed '1d' | awk '$2!='tmpfs'{print}'| awk '{total+=$3}END{print total}'")
DiskTotalNum = int(DiskTotal[0])
# 硬盤使用量
DiskUsed = runCommand("df -TP | sed '1d' | awk '$2!='tmpfs'{print}'| awk '{total+=$4}END{print total}'")
DiskUsedNum = int(DiskUsed[0])
# 硬盤空余量
DiskFree = DiskTotalNum - DiskUsedNum
# 硬盤使用比例
DiskUsedPercent = '{:.2%}'.format(DiskUsedNum / DiskTotalNum)
# 索引總量
InodeTotal = runCommand("df -iTP | sed '1d' | awk '$2!='tmpfs'{print}' | awk '{total+=$3}END{print total}' ")
InodeTotal_Num = int(InodeTotal[0])
# 索引使用量
InodeUsed = runCommand("df -iTP | sed '1d' | awk '$2!='tmpfs'{print}' | awk '{total+=$4}END{print total}' ")
InodeUsed_Num = int(InodeUsed[0])
# 索引剩余量
InodeFree = InodeTotal_Num - InodeUsed_Num
# 索引使用比例
InodePercent = '{:.2%}'.format(InodeUsed_Num / InodeTotal_Num)
res = {
'磁盤總覽': DiskAllInfo,
'硬盤總量(GB)': int(DiskTotalNum / 1024 / 1024),
'硬盤使用量(GB)': int(DiskUsedNum / 1024 / 1024),
'硬盤空余量(GB)': int(DiskFree / 1024 / 1024),
'硬盤使用比例(%)': DiskUsedPercent,
'索引總量(MB)': int(InodeTotal_Num / 1021),
'索引使用量(MB)': int(InodeUsed_Num / 1021),
'索引剩余量(MB)': int(InodeFree / 1021),
'索引使用比例(%)': InodePercent,
}
return res
def getNetworkStatus():
"""
網(wǎng)絡檢查
"""
GATEWAY = runCommand("ip route | grep default | awk '{print $3}'")
DNS = runCommand("grep nameserver /etc/resolv.conf| grep -v '#' | awk '{print $2}' | tr '\n' ',' | sed 's/,$//'")
IP = runCommand(
"ip -f inet addr | grep -v 127.0.0.1 | grep inet | awk '{print $NF,$2}' | tr '\n' ',' | sed 's/,$//'")
# TODO 語句有問題會報錯,sed的錯誤,需要檢查下執(zhí)行情況
# MAC = runCommand("ip link | grep -v 'LOOPBACK\|loopback' | awk '{print $2}' | sed 'N;s/\n//' | tr '\n' ',' | sed 's/,$//'")
res = {
'GATEWAY': GATEWAY,
'DNS': DNS,
'IP': IP
# 'MAC': MAC
}
return res
def getUserStatus():
"""
所有用戶和空密碼用戶
"""
all_user = runCommand("awk -F':' '{ print $1}' /etc/passwd")
empty_passwd_user = runCommand("getent shadow | grep -Po '^[^:]*(?=::)'")
res = {
'所有用戶名': all_user,
'空密碼用戶': empty_passwd_user
}
return res
def getJdkStatus():
"""
jdk信息
"""
jdkInfo = runCommand("java -version 2>&1")
res = {
'jdk信息': jdkInfo
}
return res
def getFirewallStatus():
"""
防火墻
"""
firewall = runCommand("firewall-cmd --state 2>&1")
# 兼容 ubuntu 防火墻命令報錯 sh: not found 特殊處理
for info in firewall:
if "not found" in info:
firewall = runCommand("ufw status")
res = {
'防火墻狀態(tài)': firewall
}
return res
def sshStatus():
"""
ssh 檢查
"""
sshActive = runCommand("systemctl is-active sshd.service")
sshNetstat = runCommand("sudo netstat -atlunp | grep sshd")
res = {
'ssh開啟狀態(tài)': sshActive,
'ssh運行情況': sshNetstat
}
return res
def ntpStatus():
"""
ntp 檢查
"""
ntpActive = runCommand("systemctl is-active ntpd")
res = {
'ntp運行情況': ntpActive
}
return res
def dockerStatus():
"""
docker 檢查
"""
dk_version = runCommand("docker -v")
dk_stats = []
for info in dk_version:
if "version" not in info:
dk_version = "未安裝docker"
else:
lines = os.popen(
"docker stats --all --no-stream").readlines()
for line in lines:
dk_stats.append(line.replace('\n', ''))
dp_version = runCommand("docker-compose --version")
for info in dp_version:
if "version" not in info:
dp_version = "未安裝docker-compose"
res = {
'docker version': dk_version,
'docker-compose version': dp_version,
'docker stats': dk_stats
}
return res
def createReportFile(name, text):
"""
創(chuàng)建report的txt文件,并寫入數(shù)據(jù)
"""
report_dir = os.getcwd() + os.sep + "report" + os.sep
# 判斷當前路徑是否存在,沒有則創(chuàng)建new文件夾
if not os.path.exists(report_dir):
os.makedirs(report_dir)
# 在當前py文件所在路徑下的new文件中創(chuàng)建txt
report_file = report_dir + name + '.txt'
# 打開文件,open()函數(shù)用于打開一個文件,創(chuàng)建一個file對象,相關的方法才可以調(diào)用它進行讀寫。
file = open(report_file, 'w')
# 寫入內(nèi)容信息
file.write(text)
file.close()
print('report_file create success', report_file)
def printSinfcloud():
print("+------------------------------------------------+")
print("| 歡迎使用SinfCloud自動巡檢工具 |")
print("| ____ _ __ ____ _ _ |")
print("|/ ___|(_)_ __ / _|/ ___| | ___ _ _ __| | |")
print("|\___ \| | _ \| |_| | | |/ _ \| | | |/ _ | |")
print("| ___) | | | | | _| |___| | (_) | |_| | (_| | |")
print("||____/|_|_| |_|_| \____|_|\___/ \__,_|\__,_| |")
print("| |")
print("+------------------------------------------------+")
if __name__ == '__main__':
printSinfcloud()
outputFileName = time.strftime('%Y-%m-%d', time.localtime(time.time())) + "_report"
report = list()
report.append(getSystemInfo())
report.append(getSystemStatus())
report.append(getCpuStatus())
report.append(getMemStatusSimple())
report.append(getDiskStatus())
report.append(getNetworkStatus())
report.append(getUserStatus())
report.append(getJdkStatus())
report.append(getFirewallStatus())
report.append(sshStatus())
report.append(ntpStatus())
report.append(dockerStatus())
createReportFile(outputFileName,
json.dumps(report, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))到此這篇關于Python實現(xiàn)Linux服務器自動巡檢腳本的文章就介紹到這了,更多相關Python Linux自動巡檢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
如何在Python中用三階指數(shù)平滑模型對金融數(shù)據(jù)集進行擬合與預測
這篇文章主要介紹了如何在Python中用三階指數(shù)平滑模型對金融數(shù)據(jù)集進行擬合與預測,本次實驗數(shù)據(jù)為10列金融數(shù)據(jù)集,且相互之間獨立,需要分別擬合預測,需要的朋友可以參考下2023-03-03
python代碼 if not x: 和 if x is not None: 和 if not x is None:使用
這篇文章主要介紹了python代碼 if not x: 和 if x is not None: 和 if not x is None:使用介紹,需要的朋友可以參考下2016-09-09
Python 標準庫zipfile將文件夾加入壓縮包的操作方法
Python zipfile 庫可用于壓縮/解壓 zip 文件. 本文介紹一下如何創(chuàng)建壓縮包,對Python zipfile壓縮包相關知識感興趣的朋友一起看看吧2021-09-09
Python使用latexify模塊實現(xiàn)將代碼為數(shù)學公式
latexify 是一個輕量級的 Python 模塊,可以將 Python 代碼轉換為 LaTeX 格式的數(shù)學表達式,這篇文章就來和大家探索一下如何使用latexify模塊實現(xiàn)將代碼為數(shù)學公式吧2023-12-12

