Python 监控脚本

整体通过psutil模块动态获取资源信息。下为示例图:

 #!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time: 2019-07-01 17:23:01
# @Author: xiangsikai
#-----------------------------------------
# Start margin size: 100x100
# The startup script: python3 monitoring.py
# Close the script: q
# matters need attention: forbid "ctrl + c"
#-----------------------------------------
# ############# debugger ############## #
# from pudb import set_trace;set_trace()# import queue
import threading
import os
import sys
import psutil
import time
import term
import tty
import termios
import datetime
import platform class Run(object):
"""Perform each task""" def run(self):
"""Enable multithreading to complete tasks""" global flag
flag = True thread_get = threading.Thread(
target=Manage("get").mamage_get)
thread_cpu = threading.Thread(
target=Manage("frames_cpu").mamage_put)
thread_memory = threading.Thread(
target=Manage("frames_memory").mamage_put)
thread_swap = threading.Thread(
target=Manage("frames_swap").mamage_put)
thread_disk = threading.Thread(
target=Manage("frames_disk").mamage_put)
thread_diskio = threading.Thread(
target=Manage("frames_diskio").mamage_put)
thread_system = threading.Thread(
target=Manage("frames_system").mamage_put)
thread_network = threading.Thread(
target=Manage("frames_network").mamage_put)
thread_version = threading.Thread(
target=Manage("frames_version").mamage_put)
thread_process = threading.Thread(
target=Manage("frames_process").mamage_put) thread_process.start()
thread_cpu.start()
thread_memory.start()
thread_swap.start()
thread_disk.start()
thread_diskio.start()
thread_network.start()
thread_system.start()
thread_version.start()
thread_get.start() # Short execution framewor
frames = Frames()
time.sleep(1)
term.clear()
term.pos(1, 0)
frames.header()
term.pos(2, 0)
time.sleep(3) # Judge the input and exit
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(sys.stdin.fileno())
quit = sys.stdin.read(1)
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
if len(quit) == 1:
flag = False
time.sleep(1)
que.queue.clear()
# Reduction of the cursor
os.system("echo -e \033[?25h")
term.pos(1, 0)
term.clear() class Manage(object):
"""Manage the operations of each class""" def __init__(self, name):
self.name = name def mamage_put(self):
"""Threads are specified by reflection as methods""" frames = Frames()
while flag:
getattr(frames, self.name)()
time.sleep(0.1) def mamage_get(self):
"""Output the value under the fixed method according to the category""" while flag:
value = que.get()
value_type = value["type"]
if value_type == "cpu":
self.mamage_cpu(value)
if value_type == "memory":
self.mamage_memory(value)
if value_type == "swap":
self.mamage_swap(value)
if value_type == "disk":
self.mamage_disk(value)
if value_type == "diskio":
self.mamage_diskio(value)
if value_type == "network":
self.mamage_network(value)
if value_type == "system":
self.mamage_system(value)
if value_type == "version":
self.mamage_version(value)
if value_type == "process":
self.mamage_process(value)
# Second judgment to clean message queue
if flag == False:
que.queue.clear()
time.sleep(0.01) def mamage_cpu(self, value):
"""CPU output format""" term.pos(7, 4)
term.write("CPU", term.bold)
term.pos(7, 19)
term.write(" ")
term.pos(7, 8)
# Output progress bar
percent = ("%s%%" % (int(value["total"])))
term.write("[%-10s]%s" % ("|" * int(value["total"]/10), percent))
term.pos(7, 30)
term.write("-CPU-", term.bold)
term.pos(7, 45)
term.write(" ")
term.pos(7, 39)
term.write("total: %s %%" % (round(value["total"], 1)))
term.pos(7, 60)
term.write(" ")
term.pos(7, 55)
term.write("used: %s %%" % (round(value["user"], 1)))
term.pos(7, 75)
term.write(" ")
term.pos(7, 70)
term.write("syst: %s %%" % (round(value["system"], 1)))
term.pos(8, 45)
term.write(" ")
term.pos(8, 39)
term.write("iowai: %s %%" % (round(value["iowait"], 1)))
term.pos(8, 60)
term.write(" ")
term.pos(8, 55)
term.write("nice: %s %%" % (round(value["nice"], 1)))
term.pos(8, 75)
term.write(" ")
term.pos(8, 70)
term.write("idle: %s %%" % (round(value["idle"], 1)))
term.pos(7, 4) def mamage_memory(self, value):
"""Memory output format""" term.pos(9, 4)
term.write("Mem", term.bold)
term.pos(9, 8)
total = (value["used"]/value["total"]) * 100
percent = ("%s%%" % (int(total)))
term.write("[%-10s]%s" % ("|" * int(total/10), percent))
term.pos(9, 30)
term.write("-MEM-", term.bold)
term.pos(9, 39)
term.write("total: %s MB" % (int(value["total"])))
term.pos(9, 55)
term.write("used: %s MB" % (int(value["used"])))
term.pos(9, 70)
term.write("free: %s MB" % (int(value["free"])))
term.pos(10, 39)
term.write("activ: %s MB" % (int(value["active"])))
term.pos(10, 55)
term.write("buff: %s MB" % (int(value["buffers"])))
term.pos(10, 70)
term.write("cach: %s MB" % (int(value["cached"])))
term.pos(9, 4) def mamage_swap(self, value):
"""Swap output format""" term.pos(11, 3)
term.write("Swap", term.bold)
term.pos(11, 8)
# Determine if the value is 0 and the exception is caught
try:
total = (int(value["used"])/int(value["total"])) * 100
except ZeroDivisionError:
total = 0
percent = ("%s%%" % (int(total)))
term.write("[%-10s]%s" % ("|" * int(total/10), percent))
term.pos(11, 30)
term.write("-Swap-", term.bold)
term.pos(11, 39)
term.write("total: %s MB" % (int(value["total"])))
term.pos(11, 55)
term.write("used: %s MB" % (int(value["used"])))
term.pos(11, 70)
term.write("free: %s MB" % (int(value["free"])))
term.pos(12, 41)
term.write("sin: %s MB" % (int(value["sin"])))
term.pos(12, 55)
term.write("sout: %s MB" % (int(value["sout"])))
term.pos(12, 70)
term.write("perc: %s %%" % (str(value["percent"])))
term.pos(11, 3) def mamage_disk(self, value):
"""Disk output format""" term.pos(13, 3)
term.write("Disk", term.bold)
term.pos(13, 8)
total = (value["used"]/value["total"]) * 100
percent = ("%s%%" % (int(total)))
term.write("[%-10s]%s" % ("|" * int(total/10), percent))
term.pos(13, 30)
term.write("-Disk-", term.bold)
term.pos(13, 39)
term.write("total: %s GB" % (int(value["total"])))
term.pos(13, 55)
term.write("used: %s GB" % (int(value["used"])))
term.pos(13, 70)
term.write("free: %s GB" % (int(value["free"])))
term.pos(13, 3) def mamage_system(self, value):
"""System output format""" day, now = time.strftime("%Y-%m-%d %H:%M:%S").split()
course_pid = psutil.pids()
course_count = 0
for i in course_pid:
course_count = course_count + 1 term.pos(2, 4)
term.write("USER: " + str(value["username"]))
term.pos(2, 16)
term.write("T: " + str(value["terminal"]))
term.pos(2, 28)
term.write("-")
term.pos(2, 33)
term.write("Process: " + str(course_count))
term.pos(2, 48)
term.write("-")
term.pos(2, 53)
term.write("BootTime: " + str(value["BootTime"]))
term.pos(4, 4)
term.write("HOST: " + str(value["hostname"]))
term.pos(4, 28)
term.write("-")
term.pos(4, 32)
term.write("Sec: " + str(now))
term.pos(4, 48)
term.write("-")
term.pos(4, 53)
term.write("LogiTime: " + str(value["started"]))
term.pos(2, 3) def mamage_network(self, value):
"""Network output format""" term.pos(20, 68)
term.clearLineFromPos()
term.write("TX: " + str(round(value["send"], 2)) + " KB")
term.pos(20, 88)
term.write("|", term.bold)
term.pos(21, 68)
term.clearLineFromPos()
term.write("RX: " + str(round(value["recv"], 2)) + " KB")
term.pos(21, 88)
term.write("|", term.bold)
term.pos(22, 67)
term.write("TXP: " + str(value["packets_sent"]))
term.pos(23, 67)
term.write("TXP: " + str(value["packets_recv"]))
term.pos(20, 68) def mamage_diskio(self, value):
"""Disk IO output format""" term.pos(31, 68)
term.clearLineFromPos()
term.write("Read: " + str(round(value["read"], 2)) + " KB")
term.pos(31, 88)
term.write("|", term.bold)
term.pos(32, 68)
term.clearLineFromPos()
term.write("Wrtn: " + str(round(value["write"], 2)) + " KB")
term.pos(32, 88)
term.write("|", term.bold)
term.pos(33, 68)
term.write("Rsec: " + str(value["read_count"]))
term.pos(34, 68)
term.write("Wsec: " + str(value["write_count"]))
term.pos(35, 69)
term.write("Tps: " + str(value["tps"]))
term.pos(32, 68) def mamage_process(self, value):
"""Process output format""" value = value["key"]
count = 19
# Loop outputs each process
for v in value:
term.pos(count, 3)
term.write(v["user"])
term.pos(count, 11)
term.write(str(v["pid"]))
term.pos(count, 18)
term.write(str(v["cpu"]))
term.pos(count, 26)
term.write(str(round(v["memory"], 2)))
term.pos(count, 34)
term.write(str(v["threads"]))
term.pos(count, 42)
term.write(str(v["name"]))
count = count + 1
term.pos(18, 4) def mamage_version(self, value):
"""Version of the output""" term.pos(16, 3)
term.write(value["system"])
term.pos(16, 10)
term.write(value["version"]) class Frames(object):
"""Terminal screen module""" def header(self):
"""Frame structure format output""" # upper part
bold = term.format("#", term.bold)
frame_up = bold.ljust(100, "=")
frame_up_format = term.format(frame_up)
term.write(frame_up_format+bold) # center section 1
term.pos(3, 0)
frame_three = term.format("+".ljust(87, "-"))
term.write(frame_three+"+") # center section 2
term.pos(5, 0)
frame_five = term.format("+".ljust(87, "-"))
term.write(frame_five+"+") # center section 3
frame_centre1 = "+".ljust(87, "=")
term.pos(15, 0)
term.write(frame_centre1+"+") # center section 4
frame_centre2 = "+".ljust(59, "=")
term.pos(17, 0)
term.write(frame_centre2)
term.pos(17, 88)
term.write("|", term.bold) # Producer information
kevin = "Kevin.Xiang"
term.pos(16, 47)
term.write(kevin) # next part
frame_down = bold.ljust(100, "=")
term.pos(39, 0)
term.write(frame_down+bold) # border style
for i1 in range(7, 14):
term.pos(i1, 26)
term.write("|") for i2 in range(16, 39):
term.pos(i2, 60)
term.write("|", term.bold) # Border output style IO
term.pos(16, 61)
frame_back = term.format("".rjust(27, " "), term.bgwhite)
term.write(frame_back)
term.pos(16, 71)
frame_network = term.format("NETWORK", term.black, term.bgwhite)
term.write(frame_network)
term.pos(27, 61)
term.write(frame_back)
term.pos(27, 73)
frame_disk = term.format("DISK", term.black, term.bgwhite)
term.write(frame_disk) # Process output style
term.pos(18, 0)
space = "".center(4, " ")
frame = term.format(" USER"
+ space
+ "PID"
+ space
+ "CPU%"
+ space
+ "MEM%"
+ space
+ "THRE"
+ space
+ "NAME", term.bold)
term.write(frame) # Border output style
list_down = [2, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38]
for i3 in list_down:
term.pos(i3, 0)
term.write("|", term.bold)
term.pos(i3, 88)
term.write("|", term.bold) def frames_cpu(self):
"""CPU statistics""" t1 = psutil.cpu_times()
time.sleep(1)
t2 = psutil.cpu_times() value = []
for v1, v2 in zip(t1, t2):
value.append(v2-v1) count = 0
for v in value:
count = count + v user = value[0]
nice = value[1]
system = value[2]
idle = value[3]
iowait = value[4]
irq = value[5]
softirq = value[6]
steal = value[7]
guest = value[8]
guest_nice = value[9]
total = count cpu = Cpu(user, nice, system, idle, iowait,
irq, softirq, steal, guest, guest_nice, total) cpu_out = {
"total": cpu.cpu_total(),
"user": cpu.cpu_user(),
"system": cpu.cpu_system(),
"idle": cpu.cpu_idle(),
"iowait": cpu.cpu_iowait(),
"nice": cpu.cpu_nice(),
"type": "cpu"
} que.put(cpu_out) def frames_memory(self):
"""Memory statistics""" time.sleep(1)
value = psutil.virtual_memory()
used = value.used
free = value.free
active = value.active
inactive = value.inactive
buffers = value.buffers
cached = value.cached
total = value.total memory = Memory(used, free, active, inactive,
buffers, cached, total) memory_out = {
"total": memory.memory_total(),
"used": memory.memory_used(),
"free": memory.memory_free(),
"active": memory.memory_active(),
"buffers": memory.memory_buffers(),
"cached": memory.memory_cached(),
"type": "memory"
}
que.put(memory_out) def frames_swap(self):
"""Swap information statistics""" time.sleep(1)
value = psutil.swap_memory()
used = value.used
free = value.free
sin = value.sin
sout = value.sout
total = value.total
percent = value.percent swap = Swap(used, free, sin, sout, total, percent) swap_out = {
"total": swap.swap_total(),
"used": swap.swap_used(),
"free": swap.swap_free(),
"sin": swap.swap_sin(),
"sout": swap.swap_sout(),
"percent": swap.swap_percent(),
"type": "swap"
} que.put(swap_out) def frames_disk(self):
"""Information statistics only get / contents disk""" time.sleep(1)
value = psutil.disk_usage('/')
used = value.used
free = value.free
total = value.total disk = Disk(used, free, total) disk_out = {
"total": disk.disk_total(),
"used": disk.disk_used(),
"free": disk.disk_free(),
"type": "disk"
} que.put(disk_out) def frames_diskio(self):
"""Disk IO statistics""" t1_diskio = psutil.disk_io_counters()
time.sleep(1)
t2_diskio = psutil.disk_io_counters() read = t2_diskio.read_bytes - t1_diskio.read_bytes
write = t2_diskio.write_bytes - t1_diskio.write_bytes
read_count = t2_diskio.read_count - t1_diskio.read_count
write_count = t2_diskio.write_count - t1_diskio.write_count
tps = t2_diskio.read_count + t2_diskio.write_count - \
t1_diskio.read_count - t1_diskio.write_count diskio = Diskio(read, write, read_count, write_count, tps) diskio_out = {
"read": diskio.diskio_read(),
"write": diskio.diskio_write(),
"read_count": diskio.diskio_read_count(),
"write_count": diskio.diskio_write_count(),
"tps": diskio.diskio_tps(),
"type": "diskio"
}
que.put(diskio_out) def frames_network(self):
"""Network statistics""" t1 = psutil.net_io_counters()
time.sleep(1)
t2 = psutil.net_io_counters() value = []
for v1, v2 in zip(t1, t2):
value.append(v2-v1) bytes_sent = value[0]
bytes_recv = value[1]
packets_sent = value[2]
packets_recv = value[3] network = Network(bytes_sent, bytes_recv,
packets_sent, packets_recv) network_out = {
"send": network.network_bytes_sent(),
"recv": network.network_bytes_recv(),
"packets_sent": network.network_packets_sent(),
"packets_recv": network.network_packets_recv(),
"type": "network"
} que.put(network_out) def frames_system(self):
"""System of statistical""" time.sleep(1)
boot_time = psutil.boot_time()
tty = os.popen("tty").read().replace('/dev/', '', 1).strip()
value = psutil.users()
for v in value:
if v.terminal == tty:
username = v.name
terminal = v.terminal
hostname = v.host
started = v.started
userpid = v.pid system = System(boot_time, username,
terminal, hostname, started, userpid) system_out = {
"BootTime": system.system_boot_time(),
"username": system.system_username(),
"terminal": system.system_terminal(),
"hostname": system.system_hostname(),
"started": system.system_started(),
"userpid": system.system_userpid(),
"type": "system",
}
que.put(system_out) def frames_version(self):
"""Version statistics""" time.sleep(1)
value = platform.uname()
system = value.system
localhost = value.node
kernel = value.release
sysver = value.version
machine = value.machine version = Version(system, localhost, kernel, sysver, machine) version_out = {
"system": version.version_system(),
"localhost": version.version_localhost(),
"kernel": version.version_kernel(),
"version": version.version_sysver(),
"machine": version.version_machine(),
"type": "version"
} que.put(version_out) def frames_process(self):
"""Process statistics""" time.sleep(1)
list1 = [] PID = psutil.pids() for p in PID:
try:
proc = psutil.Process(p)
user = proc.uids()[0]
name = proc.name()
pid = p
cpu = proc.cpu_percent(interval=0)
memory = proc.memory_percent()
status = proc.status()
threads = proc.num_threads() process = Process(user, name, pid, cpu,
memory, status, threads)
dist1 = {
"user": process.process_user(),
"name": process.process_name(),
"pid": process.process_pid(),
"cpu": process.process_cpu(),
"memory": process.process_memory(),
"status": process.process_status(),
"threads": process.process_threads(),
}
list1.append(dist1)
except:
continue # Arrange by key in the dictionary
list2 = sorted(list1, key=lambda x: x["threads"], reverse=True) list3 = []
count = 0
for p2 in list2:
list3.append(p2)
count = count + 1
if count == 19:
break process_out = {
"key": list3,
"type": "process"
} que.put(process_out) class Cpu(object):
""" CPU usage information """ def __init__(self, user, nice, system, idle, iowait, irq,
softirq, steal, guest, guest_nice, total): self.user = user
self.nice = nice
self.system = system
self.idle = idle
self.iowait = iowait
self.irq = irq
self.softirq = softirq
self.steal = steal
self.guest = guest
self.guest_nice = guest_nice
self.total = total def cpu_user(self):
"""User utilization""" cpu_user = (self.user / self.total) * 100
return cpu_user def cpu_nice(self):
"""Nice usage rate""" cpu_nice = (self.nice / self.total) * 100
return cpu_nice def cpu_system(self):
"""System utilization""" cpu_system = (self.system / self.total) * 100
return cpu_system def cpu_idle(self):
"""CPU available space""" cpu_idle = (self.idle / self.total) * 100
return cpu_idle def cpu_iowait(self):
"""Disk IO usage""" cpu_iowait = (self.iowait / self.total) * 100
return cpu_iowait def cpu_total(self):
"""The total usage""" cpu_total = self.user
cpu_total = ((self.total - self.idle) / self.total) * 100
return cpu_total class Memory(object):
"""Memory information details""" def __init__(self, used, free, active, inactive, buffers, cached, total): self.used = used
self.free = free
self.active = active
self.inactive = inactive
self.buffers = buffers
self.cached = cached
self.total = total def memory_used(self):
"""Memory user usage""" memory_used = self.used/1024/1024
return memory_used def memory_free(self):
"""The true remaining memory size""" memory_free = self.free/1024/1024
return memory_free def memory_active(self):
"""The memory program considers the size available to contain the cache buffer""" memory_active = self.active/1024/1024
return memory_active def memory_inactive(self):
"""Unused memory does not contain caches""" memory_inactive = self.inactive/1024/1024
return memory_inactive def memory_buffers(self):
"""Buffer usage memory""" memory_buffers = self.buffers/1024/1024
return memory_buffers def memory_cached(self):
"""Unused memory does not contain caches""" memory_cached = self.cached/1024/1024
return memory_cached def memory_total(self):
"""Total memory size""" memory_total = self.total/1024/1024
return memory_total class Swap(object):
"""Virtual memory information""" def __init__(self, used, free, sin, sout, total, percent):
self.used = used
self.free = free
self.sin = sin
self.sout = sout
self.total = total
self.percent = percent def swap_used(self):
"""Virtual memory used by users""" swap_used = self.used/1024/1024
return swap_used def swap_free(self):
"""Virtual memory remaining space""" swap_free = self.free/1024/1024
return swap_free def swap_sin(self):
"""The system accumulates the number of MB swapped in from disk""" swap_sin = self.sin/1024/1024
return swap_sin def swap_sout(self):
"""The number of MB the system accumulates from disk """ swap_sout = self.sin/1024/1024
return swap_sout def swap_total(self):
"""Total virtual memory size""" swap_total = self.total/1024/1024
return swap_total def swap_percent(self):
"""Using percentage swap""" swap_percent = self.percent
return swap_percent class Disk(object):
"""Disk resource information""" def __init__(self, used, free, total):
self.used = used
self.free = free
self.total = total def disk_used(self):
"""Disk user use GB """ disk_used = self.used/1024/1024/1024
return disk_used def disk_free(self):
"""Disk space GB""" disk_free = self.free/1024/1024/1024
return disk_free def disk_total(self):
"""Total disk size GB""" disk_total = self.total/1024/1024/1024
return disk_total class Diskio(object):
"""View disk IO""" def __init__(self, read, write, read_count, write_count, tps):
self.read = read
self.write = write
self.read_count = read_count
self.write_count = write_count
self.tps = tps def diskio_read(self):
"""A disk read kb""" diskio_read = self.read/1024
return diskio_read def diskio_write(self):
"""A disk read kb""" diskio_write = self.write/1024
return diskio_write def diskio_read_count(self):
"""Merge reads""" diskio_read_count = self.read_count
return diskio_read_count def diskio_write_count(self):
"""Merge write times""" diskio_write_count = self.write_count
return diskio_write_count def diskio_tps(self):
"""Read/write IO handles a response transaction""" diskio_tps = self.tps
return diskio_tps class Network(object):
"""Network resource information""" def __init__(self, bytes_sent, bytes_recv, packets_sent, packets_recv):
self.bytes_sent = bytes_sent
self.bytes_recv = bytes_recv
self.packets_sent = packets_sent
self.packets_recv = packets_recv def network_bytes_sent(self):
"""The network sends packets in kilobytes per second""" network_bytes_sent = self.bytes_sent/1024
return network_bytes_sent def network_bytes_recv(self):
"""The network receives traffic in kilobytes per second""" network_bytes_recv = self.bytes_recv/1024
return network_bytes_recv def network_packets_sent(self):
"""Number of packets sent per second""" network_packets_sent = self.packets_sent
return network_packets_sent def network_packets_recv(self):
"""Number of packets received per second""" network_packets_recv = self.packets_recv
return network_packets_recv class System(object):
"""System resource information""" def __init__(self, boot_time, username, terminal, hostname, started, userpid):
self.boot_time = boot_time
self.username = username
self.terminal = terminal
self.hostname = hostname
self.started = started
self.userpid = userpid def system_boot_time(self):
"""System startup time""" system_boot_time = datetime.datetime.fromtimestamp(
self.boot_time).strftime("%Y-%m-%d %H:%M:%S")
return system_boot_time def system_username(self):
"""Current logged-in user""" system_username = self.username
return system_username def system_terminal(self):
"""User access terminal""" system_terminal = self.terminal
return system_terminal def system_hostname(self):
"""Connect user IP""" system_hostname = self.hostname
return system_hostname def system_started(self):
"""User time""" system_started = datetime.datetime.fromtimestamp(
self.started).strftime("%Y-%m-%d %H:%M:%S") return system_started def system_userpid(self):
"""User pid""" system_userpid = self.userpid
return system_userpid class Version(object):
"""system version""" def __init__(self, system, localhost, kernel, sysver, machine):
self.system = system
self.localhost = localhost
self.kernel = kernel
self.sysver = sysver
self.machine = machine def version_system(self):
"""system type""" version_system = self.system
return version_system def version_localhost(self):
"""Localhost name""" version_localhost = self.localhost
return version_localhost def version_kernel(self):
"""Kernel version""" version_kernel = self.kernel
return version_kernel def version_sysver(self):
"""system version""" version_sysver = self.sysver
return version_sysver def version_machine(self):
"""System digits""" version_machine = self.machine
return version_machine class Process(object):
"""Process information""" def __init__(self, user, name, pid, cpu, memory, status, threads):
self.user = user
self.name = name
self.pid = pid
self.cpu = cpu
self.memory = memory
self.status = status
self.threads = threads def process_user(self):
process_user = os.popen(
"getent passwd %s | awk -F':' '{print $1}'" % (self.user))
user = process_user.read()
process_user = user return process_user def process_name(self):
"""process name""" process_name = self.name
return process_name def process_pid(self):
"""process pid""" process_pid = self.pid
return process_pid def process_cpu(self):
"""process cpu""" process_cpu = self.cpu return process_cpu def process_memory(self):
"""process memory""" process_memory = self.memory
return process_memory def process_status(self):
"""process status""" process_status = self.status
return process_status def process_threads(self):
"""process threads""" process_threads = self.threads
return process_threads if __name__ == '__main__':
que = queue.Queue()
start = Run()
os.system("echo -e \033[?25l")
start.run()

monitoring.py

Python 监控脚本的更多相关文章

  1. Python 监控脚本(硬盘、cpu、内存、网卡、进程)

    #磁盘使用率disk = psutil.disk_partitions()for i in disk:    print "磁盘:%s   分区格式:%s"%(i.device,i ...

  2. python简单的监控脚本-利用socket、psutil阻止远程主机运行特定程序

    python简单的监控脚本-利用socket.psutil阻止远程主机运行特定程序 psutil是一个跨平台的库(http://code.google.com/p/psutil/),能够轻松的实现获取 ...

  3. python监控端口脚本[jkport2.0.py]

    #!/usr/bin/env python #!coding=utf-8 import os import time import sys import smtplib from email.mime ...

  4. python 写一个类似于top的监控脚本

    最近老板给提出一个需要,项目需求大致如下:      1.用树莓派作为网关,底层接多个ZigBee传感节点,网关把ZigBee传感节点采集到的信息通过串口接收汇总,并且发送给上层的HTTP Serve ...

  5. 监控Linux服务器上python服务脚本

    提供给公司使用的测试平台这两天频繁地挂掉,影响到相关同事的正常使用,决定在服务器上写个监控脚本,监控到服务挂了就启动起来,一分钟检查一次.注:后台服务使用的是python.监控脚本如下: NUM=`p ...

  6. [工具开发] keepalived使用nagios监控脚本

    最近在做开发和办公环境的高可用,采用的是keepalived:keepalived基于Linux内核支持的LVS,既能实现高可用,又能实现负载均衡,非常实用. keepalived监控服务状态时可以用 ...

  7. python 小脚本升级-- 钉钉群聊天机器人

    一则小脚本(工作中用) 在这篇文章中写的监控的脚本,发送监控的时候 是利用的邮箱,其实在实际,邮箱查收有着不方便性,于是乎升级, 我们工作中,经常用钉钉,那么如果要是能用到钉钉多好,这样我们的监控成功 ...

  8. python监控微信报警

    微信接口调用代码: #coding=utf8 import itchat from flask import Flask, request itchat.auto_login(enableCmdQR= ...

  9. shell监控脚本

    序言: 前几天一好友问我服务器监控怎么做?你们公司的监控是怎么做的?有什么开源的监控软件推荐?常见的开源的监控软件当然首先推荐ZABBIX,分布式够强大,而且很多公司都在用,我问他具体什么需求,能监控 ...

随机推荐

  1. AHB总线协议(二)

    下图是8拍回环字突发传输:地址将在 32 字节边界处回环因此地址 0x3C 之后的地址是 0x20. 下图是8增量半字突发传输,所以地址每次增加 2 个字节并且突发在递增因此地址连续增加通过了 16 ...

  2. Oracle 11.2.0.4单实例打补丁

    Oracle 11.2.0.4单实例打PSU,OJVM PSU补丁快速参考 写在前面: ·         1.Oracel打每个补丁的操作有时存在差异,所以不管多熟悉,都应该在打任何补丁之前阅读新补 ...

  3. redis不能保存bean对象

    可用JSON转为json格式 // 2.3 将用户信息存储在redis中 String memberToJson = JSON.toJSON(member).toString(); 需要maven坐标 ...

  4. 【转载】浅析从外部访问 Kubernetes 集群中应用的几种方式

    一般情况下,Kubernetes 的 Cluster Network 是属于私有网络,只能在 Cluster Network 内部才能访问部署的应用.那么如何才能将 Kubernetes 集群中的应用 ...

  5. jenkins中 Slave使用Docker

    原因就不说了,网上的自动化测试Docker教程太不靠谱,还是学学官网吧. 目的: 在现在各种虚拟化的大条件下,还在建立N个节点机器或节点机器搞N个并发,是不是太不方便了. 如果一个机器搞N个并发,在自 ...

  6. ElementUI_NodeJS环境搭建

    ElementUI简介 我们学习VUE,知道它的核心思想式组件和数据驱动,但是每一个组件都需要自己编写模板,样式,添加事件,数据等是非常麻烦的, 所以饿了吗推出了基于VUE2.0的组件库,它的名称叫做 ...

  7. ESA2GJK1DH1K基础篇: 阿里云物联网平台: 云平台显示单片机采集的温湿度数据,控制设备继电器(基于GPRS模块,AT指令TCP_MQTT通信)

    实现的功能 上一节是使用的Wi-Fi模块连接的阿里云平台,这节呢咱用GPRS连接. 阿里云的设备配置还是按照上一节的,有个温度,有个湿度. 测试 一,打开单片机程序 二,根据自己的修改,然后下载进去单 ...

  8. [FJOI2018]所罗门的宝藏

    大概是最后一篇题解,其实只是想颓废一下打个故事 据古代传说记载,所罗门王即是智慧的代表,又是财富的象征.他建立了强大而富有的国家,聚集了大批的黄金象牙和钻石,并把这些价值连城的珍宝藏在一个神秘的地方, ...

  9. (近万字)一篇文章带你了解HTML5和CSS3开发基础与应用-适合前端面试必备

    作者 | Jeskson 来源 | 达达前端小酒馆 HTML5和CSS3开发基础与应用,详细说明HTML5的新特性和新增加元素,CSS3的新特性,新增加的选择器,新的布局,盒子模型,文本,边框,渐变, ...

  10. Windows下ActiveMq安装与使用

    一.activeMq安装与启动 Apache Active MQ的官网 :http://activemq.apache.org/ 下载地址: http://activemq.apache.org/ac ...