4
4
import shutil
5
5
import platform
6
6
import sqlite3
7
- import os
8
- import re
9
7
from datetime import datetime
10
8
from discord .ext import commands , tasks
11
9
from config .settings import SYSTEM_CHANNEL_ID
@@ -176,82 +174,22 @@ def store_message_id(self, guild_id, message_id, channel_id=None):
176
174
177
175
def get_cpu_usage (self ):
178
176
"""Retrieve CPU usage percentage."""
179
- try :
180
- return psutil .cpu_percent (interval = 1 )
181
- except Exception as e :
182
- logger .error (f"Error getting CPU usage: { e } " )
183
- return 0.0
184
-
185
- def get_cpu_info (self ):
186
- """Get CPU information using multiple methods to work in Docker."""
187
- cpu_info = "Unknown"
188
-
189
- # Try several methods to get CPU info
190
- try :
191
- # Method 1: platform.processor() - doesn't always work in Docker
192
- processor = platform .processor ()
193
- if processor and len (processor .strip ()) > 0 :
194
- cpu_info = processor
195
- logger .debug (f"CPU info from platform.processor(): { cpu_info } " )
196
- return cpu_info
197
-
198
- # Method 2: Try to read from /proc/cpuinfo directly
199
- if os .path .exists ("/proc/cpuinfo" ):
200
- with open ("/proc/cpuinfo" , "r" ) as f :
201
- for line in f :
202
- if line .startswith ("model name" ):
203
- cpu_info = line .split (":" , 1 )[1 ].strip ()
204
- logger .debug (f"CPU info from /proc/cpuinfo: { cpu_info } " )
205
- return cpu_info
206
-
207
- # Method 3: Try lscpu command (may not be available in all containers)
208
- import subprocess
209
-
210
- try :
211
- result = subprocess .run (
212
- ["lscpu" ], capture_output = True , text = True , timeout = 1
213
- )
214
- if result .returncode == 0 :
215
- for line in result .stdout .splitlines ():
216
- if "Model name:" in line :
217
- cpu_info = line .split (":" , 1 )[1 ].strip ()
218
- logger .debug (f"CPU info from lscpu: { cpu_info } " )
219
- return cpu_info
220
- except (subprocess .SubprocessError , FileNotFoundError ) as e :
221
- logger .debug (f"lscpu command failed: { e } " )
222
-
223
- except Exception as e :
224
- logger .debug (f"Error getting CPU info: { e } " )
225
-
226
- # Final fallback
227
- return cpu_info
177
+ return psutil .cpu_percent (interval = 1 )
228
178
229
179
def get_memory_usage (self ):
230
180
"""Retrieve memory usage in MB."""
231
- try :
232
- memory = psutil .virtual_memory ()
233
- return memory .used / (1024 * 1024 ), memory .total / (1024 * 1024 )
234
- except Exception as e :
235
- logger .error (f"Error getting memory usage: { e } " )
236
- return 0 , 1 # Return dummy values to avoid division by zero
181
+ memory = psutil .virtual_memory ()
182
+ return memory .used / (1024 * 1024 ), memory .total / (1024 * 1024 )
237
183
238
184
def get_disk_usage (self ):
239
185
"""Retrieve disk usage statistics."""
240
- try :
241
- disk = shutil .disk_usage ("/" )
242
- return disk .used / (1024 * 1024 * 1024 ), disk .total / (1024 * 1024 * 1024 )
243
- except Exception as e :
244
- logger .error (f"Error getting disk usage: { e } " )
245
- return 0 , 1 # Return dummy values to avoid division by zero
186
+ disk = shutil .disk_usage ("/" )
187
+ return disk .used / (1024 * 1024 * 1024 ), disk .total / (1024 * 1024 * 1024 )
246
188
247
189
def get_network_stats (self ):
248
190
"""Retrieve network statistics."""
249
- try :
250
- stats = psutil .net_io_counters ()
251
- return stats .bytes_sent , stats .bytes_recv
252
- except Exception as e :
253
- logger .error (f"Error getting network stats: { e } " )
254
- return 0 , 0 # Return dummy values
191
+ stats = psutil .net_io_counters ()
192
+ return stats .bytes_sent , stats .bytes_recv
255
193
256
194
def check_sqlite_connection (self ):
257
195
"""Check the connection status of the SQLite database."""
@@ -265,63 +203,6 @@ def check_sqlite_connection(self):
265
203
logger .error (f"SQLite connection error: { e } " )
266
204
return "Offline"
267
205
268
- def get_system_uptime (self ):
269
- """Get system uptime in a human-readable format."""
270
- uptime_seconds = 0
271
- uptime_str = "Unknown"
272
-
273
- # Try multiple methods to get uptime
274
- try :
275
- # Method 1: Read from /proc/uptime (Linux)
276
- if os .path .exists ("/proc/uptime" ):
277
- with open ("/proc/uptime" , "r" ) as f :
278
- uptime_seconds = int (float (f .read ().split ()[0 ]))
279
-
280
- # Method 2: Use psutil if available
281
- if uptime_seconds == 0 :
282
- try :
283
- uptime_seconds = int (time .time () - psutil .boot_time ())
284
- except :
285
- pass
286
-
287
- # Method 3: For Windows systems
288
- if uptime_seconds == 0 and platform .system () == "Windows" :
289
- try :
290
- import ctypes
291
-
292
- class LASTINPUTINFO (ctypes .Structure ):
293
- _fields_ = [
294
- ("cbSize" , ctypes .c_uint ),
295
- ("dwTime" , ctypes .c_uint ),
296
- ]
297
-
298
- GetTickCount = ctypes .windll .kernel32 .GetTickCount
299
- GetTickCount .restype = ctypes .c_uint
300
- uptime_ms = GetTickCount ()
301
- uptime_seconds = uptime_ms // 1000
302
- except :
303
- pass
304
-
305
- # Format uptime if we got it
306
- if uptime_seconds > 0 :
307
- days , remainder = divmod (uptime_seconds , 86400 )
308
- hours , remainder = divmod (remainder , 3600 )
309
- minutes , seconds = divmod (remainder , 60 )
310
-
311
- uptime_str = ""
312
- if days > 0 :
313
- uptime_str += f"{ days } d "
314
- if hours > 0 or days > 0 :
315
- uptime_str += f"{ hours } h "
316
- if minutes > 0 or hours > 0 or days > 0 :
317
- uptime_str += f"{ minutes } m "
318
- uptime_str += f"{ seconds } s"
319
-
320
- except Exception as e :
321
- logger .debug (f"Error getting system uptime: { e } " )
322
-
323
- return uptime_str
324
-
325
206
def create_embed (self ):
326
207
"""Create a beautifully styled embed for system information."""
327
208
try :
@@ -330,9 +211,8 @@ def create_embed(self):
330
211
disk_used , disk_total = self .get_disk_usage ()
331
212
network_sent , network_recv = self .get_network_stats ()
332
213
cpu_usage = self .get_cpu_usage ()
333
- cpu_info = self . get_cpu_info ()
214
+ cpu_info = platform . processor ()
334
215
sqlite_status = self .check_sqlite_connection ()
335
- uptime_str = self .get_system_uptime ()
336
216
337
217
# Calculate usage percentages
338
218
memory_percent = (
@@ -381,7 +261,7 @@ def get_status_emoji(percent):
381
261
embed .add_field (
382
262
name = "CPU Usage" ,
383
263
value = f"\n <:icon_reply:993231553083736135> { cpu_emoji } `{ cpu_bar } ` **{ cpu_usage :.1f} %**\n "
384
- f"<:icon_reply:993231553083736135> Processor: { cpu_info } \n " ,
264
+ f"<:icon_reply:993231553083736135> Processor: { cpu_info . split ()[ 0 ] if len ( cpu_info . split ()) > 0 else 'Unknown' } \n " ,
385
265
inline = False ,
386
266
)
387
267
embed .add_field (
@@ -450,8 +330,22 @@ def get_status_emoji(percent):
450
330
inline = False ,
451
331
)
452
332
453
- # Add system uptime
454
- if uptime_str != "Unknown" :
333
+ # Add system uptime if available
334
+ try :
335
+ uptime_seconds = int (float (open ("/proc/uptime" ).read ().split ()[0 ]))
336
+ days , remainder = divmod (uptime_seconds , 86400 )
337
+ hours , remainder = divmod (remainder , 3600 )
338
+ minutes , seconds = divmod (remainder , 60 )
339
+
340
+ uptime_str = ""
341
+ if days > 0 :
342
+ uptime_str += f"{ days } d "
343
+ if hours > 0 or days > 0 :
344
+ uptime_str += f"{ hours } h "
345
+ if minutes > 0 or hours > 0 or days > 0 :
346
+ uptime_str += f"{ minutes } m "
347
+ uptime_str += f"{ seconds } s"
348
+
455
349
embed .add_field (
456
350
name = "System Uptime" ,
457
351
value = f"\n <:icon_reply:993231553083736135> **{ uptime_str } **" ,
@@ -462,6 +356,49 @@ def get_status_emoji(percent):
462
356
value = "" ,
463
357
inline = False ,
464
358
)
359
+ except :
360
+ # Uptime might not be available on all systems (especially Windows)
361
+ try :
362
+ # For Windows systems
363
+ import ctypes
364
+
365
+ class LASTINPUTINFO (ctypes .Structure ):
366
+ _fields_ = [
367
+ ("cbSize" , ctypes .c_uint ),
368
+ ("dwTime" , ctypes .c_uint ),
369
+ ]
370
+
371
+ GetTickCount = ctypes .windll .kernel32 .GetTickCount
372
+ GetTickCount .restype = ctypes .c_uint
373
+
374
+ uptime_ms = GetTickCount ()
375
+ uptime_seconds = uptime_ms // 1000
376
+
377
+ days , remainder = divmod (uptime_seconds , 86400 )
378
+ hours , remainder = divmod (remainder , 3600 )
379
+ minutes , seconds = divmod (remainder , 60 )
380
+
381
+ uptime_str = ""
382
+ if days > 0 :
383
+ uptime_str += f"{ days } d "
384
+ if hours > 0 or days > 0 :
385
+ uptime_str += f"{ hours } h "
386
+ if minutes > 0 or hours > 0 or days > 0 :
387
+ uptime_str += f"{ minutes } m "
388
+ uptime_str += f"{ seconds } s"
389
+
390
+ embed .add_field (
391
+ name = "System Uptime" ,
392
+ value = f"\n <:icon_reply:993231553083736135> **{ uptime_str } **" ,
393
+ inline = False ,
394
+ )
395
+ embed .add_field (
396
+ name = "" ,
397
+ value = "" ,
398
+ inline = False ,
399
+ )
400
+ except :
401
+ pass
465
402
466
403
embed .set_thumbnail (
467
404
url = "https://cdn.discordapp.com/emojis/1033460420587049021.png"
0 commit comments