Skip to content

Commit 2769981

Browse files
committed
add: metric collection service
1 parent efe2546 commit 2769981

File tree

8 files changed

+58
-2
lines changed

8 files changed

+58
-2
lines changed
349 Bytes
Binary file not shown.
779 Bytes
Binary file not shown.

app/handlers/basic.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,27 @@
66
def register_basic_handlers(dp):
77
@dp.message(Command("start"), IsOwner())
88
async def welcome(message: types.Message):
9-
await message.answer("Hello!")
9+
await message.answer(
10+
"Welcome! This is a bot to monitor your server.\n"
11+
"\n"
12+
"Quick start guide:\n"
13+
"/start - Starts the bot.\n"
14+
"/help - lists all of the available commands.\n"
15+
)
16+
17+
@dp.message(Command("help"), IsOwner())
18+
async def list_commands(message: types.Message):
19+
await message.answer(
20+
"All of the available commands:\n"
21+
"/start - Starts the bot.\n"
22+
"/help - lists all of the available commands.\n"
23+
)
1024

1125
@dp.message(~IsOwner())
1226
async def unauthorized(message: types.Message):
13-
await message.answer("You are not the owner!")
27+
await message.answer(
28+
f"Sorry, you are not authorized to use this bot \n"
29+
f"\n"
30+
f"However, you can create your own bot using this link:\n" \
31+
f"https://github.com/Oget565/Server-Bot-Monitor\n"
32+
)

app/services/__init__.py

Whitespace-only changes.
168 Bytes
Binary file not shown.
2.08 KB
Binary file not shown.

app/services/metrics.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import psutil
2+
p = psutil
3+
4+
class Resources:
5+
def __init__(self):
6+
pass
7+
8+
async def get_resources(self):
9+
self.mem_total = round(p.virtual_memory()[0] / (1024**3), 2)
10+
self.mem_used = round(p.virtual_memory()[3] / (1024**3), 2)
11+
self.mem_prcnt = p.virtual_memory()[2]
12+
13+
self.cpu_prcnt = p.cpu_percent()
14+
self.cpu_freq = round(p.cpu_freq()[0] / 1000, 1)
15+
16+
temps = psutil.sensors_temperatures()
17+
self.cpu_temp = None
18+
if temps:
19+
cpu_keys = ['coretemp', 'cpu_thermal', 'k10temp']
20+
for key in cpu_keys:
21+
if key in temps:
22+
cores = temps[key]
23+
self.cpu_temp = round(sum([c.current for c in cores]) / len(cores), 1)
24+
break
25+
26+
async def pack_resources(self):
27+
resources = {
28+
'mem_total': self.mem_total,
29+
'mem_used': self.mem_used,
30+
'mem_prcnt': self.mem_prcnt,
31+
'cpu_prcnt': self.cpu_prcnt,
32+
'cpu_freq': self.cpu_freq,
33+
'cpu_temp': self.cpu_temp
34+
}
35+
36+
return resources

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ idna==3.11
1111
magic-filter==1.0.12
1212
multidict==6.7.0
1313
propcache==0.4.1
14+
psutil==7.1.3
1415
pydantic==2.11.10
1516
pydantic_core==2.33.2
1617
python-dotenv==1.2.1

0 commit comments

Comments
 (0)