-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics
140 lines (132 loc) · 5.35 KB
/
metrics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Script: metrics
#
# Description: this python script collects metrics about linux server:
# - OS release
# - CPU frequency and metrics
# - Memory metrics
# - Uptime
# - Disk usage
# - Swap usage
# - Processes
#
# Requirement(s): linux, python, psutils (library) | https://psutil.readthedocs.io/
#
# Changelog: * 12/18/2020: Igor Petrenko
# - Initial release
#
# Usage:
# ./metrics os
# ./metrics cpu
# ./metrics memory
# ./metrics uptime
# ./metrics disk
# ./metrics swap
# ./metrics processes
import psutil
import datetime
import platform
import sys
from sys import argv
def os():
# OS release
print("System:",platform.system())
print("Platform:",platform.release())
print("Machine:",platform.machine())
def uptime():
# System boot time converted to human readable format
print("System boot time:",datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
def disk():
# Disk usage metrics for '/'
disk_usage_total = psutil.disk_usage('/').total
print("total %.2f GB /" % (disk_usage_total / (1024.0 ** 3)))
disk_usage_used = psutil.disk_usage('/').used
print("used %.2f GB /" % (disk_usage_used / (1024.0 ** 3)))
disk_usage_free = psutil.disk_usage('/').free
print("free %.2f GB /" % (disk_usage_free / (1024.0 ** 3)))
disk_usage_percent = psutil.disk_usage('/').percent
print("percent of disk_usage:",disk_usage_percent,"%","/")
def cpu():
# CPU frequency and metrics
cpu_freq_current = psutil.cpu_freq().current
print("CPU frequency current:",cpu_freq_current,"MHz")
print("Number of cores in system:", psutil.cpu_count())
print("Number of physical cores in system:", psutil.cpu_count(logical=False))
print("Number of logical cores in system:", psutil.cpu_count(logical=True))
cpu_user = psutil.cpu_times().user
print("system.cpu.user:",cpu_user)
cpu_system = psutil.cpu_times().system
print("system.cpu.system:",cpu_system)
cpu_idle = psutil.cpu_times().idle
print("system.cpu.idle:",cpu_idle)
cpu_nice = psutil.cpu_times().nice
print("system.cpu.nice:",cpu_nice)
cpu_iowait = psutil.cpu_times().iowait
print("system.cpu.iowait:",cpu_iowait)
cpu_irq = psutil.cpu_times().irq
print("system.cpu.irq:",cpu_irq)
cpu_softirq = psutil.cpu_times().softirq
print("system.cpu.softirq:",cpu_softirq)
cpu_steal = psutil.cpu_times().steal
print("system.cpu.steal:",cpu_steal)
cpu_guest = psutil.cpu_times().guest
print("system.cpu.guest:",cpu_guest)
def memory():
# Memory metrics
virtual_total = psutil.virtual_memory().total
print("virtual total: %.2f GB" % (virtual_total / (1024.0 ** 3)))
virtual_available = psutil.virtual_memory().available
print("virtual available: %.2f GB" % (virtual_available / (1024.0 ** 3)))
virtual_used = psutil.virtual_memory().used
print("virtual used: %.2f GB" % (virtual_used / (1024.0 ** 3)))
virtual_free = psutil.virtual_memory().free
print("virtual free: %.2f GB" % (virtual_free / (1024.0 ** 3)))
virtual_active = psutil.virtual_memory().active
print("virtual active: %.2f GB" % (virtual_active / (1024.0 ** 3)))
virtual_inactive = psutil.virtual_memory().inactive
print("virtual inactive: %.2f GB" % (virtual_inactive / (1024.0 ** 3)))
virtual_buffers = psutil.virtual_memory().buffers
print("virtual buffers: %.2f GB" % (virtual_buffers / (1024.0 ** 3)))
virtual_cached = psutil.virtual_memory().cached
print("virtual cached: %.2f GB" % (virtual_cached / (1024.0 ** 3)))
virtual_shared = psutil.virtual_memory().shared
print("virtual shared: %.2f GB" % (virtual_shared / (1024.0 ** 3)))
def swap():
# Swap usage
swap_total = psutil.swap_memory().total
print("swap total:",swap_total)
swap_used = psutil.swap_memory().used
print("swap used:",swap_used)
swap_free = psutil.swap_memory().free
print("swap free:",swap_free)
swap_percent = psutil.swap_memory().percent
print("swap percent:",swap_percent)
swap_sin = psutil.swap_memory().sin
print("swap sin:",swap_sin)
swap_sout = psutil.swap_memory().sout
print("swap sout:",swap_sout)
def processes():
curProcesses = psutil.process_iter()
print("PID | ProcessName | Username")
for eachProcess in curProcesses:
print((eachProcess.pid),"|",(eachProcess.name()),"|",(eachProcess.username()))
if __name__ == "__main__":
if len(sys.argv) > 1 and len(sys.argv) < 3:
if sys.argv[1] == "os":
os()
if sys.argv[1] == "cpu":
cpu()
if sys.argv[1] == "memory":
memory()
if sys.argv[1] == "disk":
disk()
if sys.argv[1] == "uptime":
uptime()
if sys.argv[1] == "swap":
swap()
if sys.argv[1] == "processes":
processes()
else:
sys.stderr.write("""Please enter os|cpu|memory|disk|uptime|swap|processes\n""")
sys.exit