Passed
Push — master ( 9bfda5...dc2491 )
by Anas
02:12
created

modules.pcstat   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 4

3 Functions

Rating   Name   Duplication   Size   Complexity  
A module_init() 0 4 2
A seconds_to_str() 0 5 1
A status() 0 48 1
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import CommandHandler
4
from telegram import ChatAction
5
from uptime import uptime
6
import platform
7
import datetime
8
import psutil
9
10
11
def module_init(gd):
12
    commands = gd.config["commands"]
13
    for command in commands:
14
        gd.dp.add_handler(CommandHandler(command, status))
15
16
17
def seconds_to_str(seconds):
18
    days, remainder = divmod(seconds, 86400)
19
    hours, remainder = divmod(remainder, 3600)
20
    minutes, seconds = divmod(remainder, 60)
21
    return "{} day(s) {:02}:{:02}:{:02}".format(days, hours, minutes, seconds)
22
23
24
def status(bot, update):
25
    update.message.chat.send_action(ChatAction.TYPING)
26
27
    # Uptime
28
    f = int(uptime())
29
    pcuptime = seconds_to_str(f)
30
31
    uptime_text = "Uptime: ", pcuptime
32
    uptime_text = "".join(uptime_text)
33
34
    # OS
35
    OS = platform.platform()
36
37
    # CPU info
38
    cpus = psutil.cpu_count(logical=False)
39
    cpus_log = psutil.cpu_count(logical=True)
40
    cpu = psutil.cpu_percent(interval=0.5, percpu=True)
41
42
    cpu_cores = "CPU cores; physical: ", str(cpus), ", logical: ", str(cpus_log)
43
    cpu_cores = "".join(cpu_cores)
44
    cpu_text = "CPU load: ", str(cpu)
45
    cpu_text = "".join(cpu_text)
46
47
    # RAM info
48
    mem = psutil.virtual_memory()
49
    total_mem = round(mem[0] / 1024**2)
50
    used_mem = round(mem[3] / 1024**2)
51
    # free_mem = round(mem[4] / 1024**2)
52
    used_mem_perc = round(mem[3] * 100 / mem[0], 1)
53
    # free_mem_perc = round(mem[4] * 100 / mem[0], 1)
54
55
    ram_text = "RAM usage: ", str(used_mem_perc), "% (", str(used_mem), "Mb of ", str(total_mem), "Mb)"
56
    ram_text = "".join(ram_text)
57
58
    # HDD info
59
    hdd = psutil.disk_usage("/")
60
    hdd_total = round(hdd[0] / 1024**3, 1)
61
    hdd_used = round(hdd[1] / 1024**3, 1)
62
    # hdd_free = round(hdd[2] / 1024**3, 1)
63
    hdd_perc = hdd[3]
64
65
    hdd_text = "HDD usage: ", str(hdd_perc), "% (", str(hdd_used), "Gb of ", str(hdd_total), "Gb)"
66
    hdd_text = "".join(hdd_text)
67
68
    # combine everything
69
    server_status = "💻 \nOS: " + OS + "\n" + cpu_cores + "\n" + cpu_text + "\n" + ram_text + "\n" + hdd_text + "\n" + uptime_text
70
    update.message.reply_text(server_status)
71
    print(datetime.datetime.now(), ">", "/status", ">", update.message.from_user.username)
72