LinuxMonitor 1.5.4__tar.gz → 1.5.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: LinuxMonitor
3
- Version: 1.5.4
3
+ Version: 1.5.6
4
4
  Summary: Get information and warning status of Linux server like service, port, ping, ssl certificate, disk/folder/cpu/ram/swap usage, ip connection, ... (Python and shell library, Linux ONLY)
5
5
  Home-page: https://github.com/QuentinCG/Linux-Monitor-Python-Library
6
6
  Author: Quentin Comte-Gaz
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: LinuxMonitor
3
- Version: 1.5.4
3
+ Version: 1.5.6
4
4
  Summary: Get information and warning status of Linux server like service, port, ping, ssl certificate, disk/folder/cpu/ram/swap usage, ip connection, ... (Python and shell library, Linux ONLY)
5
5
  Home-page: https://github.com/QuentinCG/Linux-Monitor-Python-Library
6
6
  Author: Quentin Comte-Gaz
@@ -71,10 +71,10 @@ def main() -> None:
71
71
  out_msg += msg
72
72
 
73
73
  out_msg += "\n"
74
- out_msg += monitoring.check_load_average(display_only_if_critical=False) + "\n"
75
- out_msg += monitoring.check_cpu_usage(display_only_if_critical=False) + "\n"
76
- out_msg += monitoring.check_ram_usage(display_only_if_critical=False) + "\n"
77
- out_msg += monitoring.check_swap_usage(display_only_if_critical=False) + "\n"
74
+ out_msg += asyncio.run(monitoring.check_load_average(display_only_if_critical=False)) + "\n"
75
+ out_msg += asyncio.run(monitoring.check_cpu_usage(display_only_if_critical=False)) + "\n"
76
+ out_msg += asyncio.run(monitoring.check_ram_usage(display_only_if_critical=False)) + "\n"
77
+ out_msg += asyncio.run(monitoring.check_swap_usage(display_only_if_critical=False)) + "\n"
78
78
  out_msg += monitoring.check_cpu_temperature(display_only_if_critical=False) + "\n"
79
79
  out_msg += monitoring.get_network_info()
80
80
  print(out_msg)
@@ -33,7 +33,7 @@ __email__ = "quentin@comte-gaz.com"
33
33
  __license__ = "MIT License"
34
34
  __copyright__ = "Copyright Quentin Comte-Gaz (2024)"
35
35
  __python_version__ = "3.+"
36
- __version__ = "1.5.4 (2024/10/27)"
36
+ __version__ = "1.5.6 (2024/11/03)"
37
37
  __status__ = "Usable for any Linux project"
38
38
 
39
39
  import json
@@ -587,7 +587,7 @@ class LinuxMonitor:
587
587
  logging.exception(msg=f"Error getting CPU name:\n{e}")
588
588
  return ""
589
589
 
590
- def check_cpu_usage(self, display_only_if_critical: bool=False) -> str:
590
+ async def check_cpu_usage(self, display_only_if_critical: bool=False) -> str:
591
591
  """
592
592
  Check the CPU usage.
593
593
 
@@ -597,6 +597,10 @@ class LinuxMonitor:
597
597
  """
598
598
  out_msg: str = ""
599
599
  try:
600
+ if display_only_if_critical and self.critical_cpu_percent > 100:
601
+ # There is no need to check for showing msg only if critical and the critical percentage is greater than the max possible
602
+ return out_msg
603
+
600
604
  # Getting average CPU usage for the last second
601
605
  cpu_percent: float = psutil.cpu_percent(percpu=False)
602
606
 
@@ -609,7 +613,7 @@ class LinuxMonitor:
609
613
  out_msg = f"- 🚨 **Critical CPU usage**:\n- **{cpu_percent:.2f}%** used on {cpu_cores} core of {cpu_info:.2f}GHz ({cpu_name})\n⚠️ **Check what is using so much CPU power** ⚠️"
610
614
 
611
615
  # If there is a critical CPU usage, we also display the top 10 processes consuming the most CPU
612
- out_msg += "\n" + self.get_ordered_processes(get_non_consuming_processes=False, order_by_ram=False, max_processes=10)
616
+ out_msg += "\n" + await self.get_ordered_processes(get_non_consuming_processes=False, order_by_ram=False, max_processes=10)
613
617
 
614
618
  logging.warning(msg=out_msg)
615
619
  elif not display_only_if_critical:
@@ -628,7 +632,7 @@ class LinuxMonitor:
628
632
 
629
633
  return out_msg
630
634
 
631
- def check_ram_usage(self, display_only_if_critical: bool=False) -> str:
635
+ async def check_ram_usage(self, display_only_if_critical: bool=False) -> str:
632
636
  """
633
637
  Check the RAM usage.
634
638
 
@@ -638,6 +642,10 @@ class LinuxMonitor:
638
642
  """
639
643
  out_msg: str = ""
640
644
  try:
645
+ if display_only_if_critical and self.critical_ram_percent > 100:
646
+ # There is no need to check for showing msg only if critical and the critical percentage is greater than the max possible
647
+ return out_msg
648
+
641
649
  # Getting RAM usage
642
650
  ram = psutil.virtual_memory()
643
651
  total_ram: float = ram.total / (2**30)
@@ -648,7 +656,7 @@ class LinuxMonitor:
648
656
  out_msg = f"- 🚨 **Critical RAM usage**:\n- Total: {total_ram:.2f}GB\n- Used: {used_ram:.2f}GB ({percent_ram:.2f}%)\n- Free: {free_ram:.2f}GB\n⚠️ **Check what is using so much RAM** ⚠️"
649
657
 
650
658
  # If there is a critical RAM usage, we also display the top 10 processes consuming the most RAM
651
- out_msg += "\n" + self.get_ordered_processes(get_non_consuming_processes=False, order_by_ram=True, max_processes=10)
659
+ out_msg += "\n" + await self.get_ordered_processes(get_non_consuming_processes=False, order_by_ram=True, max_processes=10)
652
660
 
653
661
  logging.warning(msg=out_msg)
654
662
  elif not display_only_if_critical:
@@ -667,7 +675,7 @@ class LinuxMonitor:
667
675
 
668
676
  return out_msg
669
677
 
670
- def check_load_average(self, display_only_if_critical: bool=False) -> str:
678
+ async def check_load_average(self, display_only_if_critical: bool=False) -> str:
671
679
  """
672
680
  Check the load average.
673
681
 
@@ -677,11 +685,15 @@ class LinuxMonitor:
677
685
  """
678
686
  out_msg: str = ""
679
687
  try:
688
+ if display_only_if_critical and self.critical_load_average_percent > 100:
689
+ # There is no need to check for showing msg only if critical and the critical percentage is greater than the max possible
690
+ return out_msg
691
+
680
692
  # Number of CPU cores
681
693
  num_cores: int = psutil.cpu_count()
682
694
 
683
695
  # Getting load average
684
- load_avg: Tuple[float] = psutil.getloadavg()
696
+ load_avg: Tuple[float] = psutil.getloadavg() # type: ignore
685
697
 
686
698
  # Getting an average of all 3 values
687
699
  avg_load_avg: float = 100 * sum(min(value, num_cores) for value in load_avg) / (len(load_avg) * num_cores)
@@ -690,7 +702,7 @@ class LinuxMonitor:
690
702
  out_msg = f"- 🚨 **High load average**: **{avg_load_avg:.2f}%**\n⚠️ **Check what is causing the high load average** ⚠️"
691
703
 
692
704
  # If there is a critical load average, we also display the top 10 processes consuming the most CPU
693
- out_msg += "\n" + self.get_ordered_processes(get_non_consuming_processes=False, order_by_ram=False, max_processes=10)
705
+ out_msg += "\n" + await self.get_ordered_processes(get_non_consuming_processes=False, order_by_ram=False, max_processes=10)
694
706
 
695
707
  logging.warning(msg=out_msg)
696
708
  elif not display_only_if_critical:
@@ -709,7 +721,7 @@ class LinuxMonitor:
709
721
 
710
722
  return out_msg
711
723
 
712
- def check_swap_usage(self, display_only_if_critical: bool=False) -> str:
724
+ async def check_swap_usage(self, display_only_if_critical: bool=False) -> str:
713
725
  """
714
726
  Check the SWAP usage.
715
727
 
@@ -719,6 +731,10 @@ class LinuxMonitor:
719
731
  """
720
732
  out_msg: str = ""
721
733
  try:
734
+ if display_only_if_critical and self.critical_swap_percent > 100:
735
+ # There is no need to check for showing msg only if critical and the critical percentage is greater than the max possible
736
+ return out_msg
737
+
722
738
  # Getting Swap usage
723
739
  swap = psutil.swap_memory()
724
740
  total_swap: float = swap.total / (2**30)
@@ -727,6 +743,10 @@ class LinuxMonitor:
727
743
  percent_swap: float = swap.percent
728
744
  if percent_swap >= self.critical_swap_percent:
729
745
  out_msg = f"- 🚨 **Critical SWAP usage**\n- Total: {total_swap:.2f}GB\n- Used: {used_swap:.2f}GB ({percent_swap:.2f}%)\n- Free: {free_swap:.2f}GB\n⚠️ **Check what is using so much SWAP** ⚠️"
746
+
747
+ # If there is a critical SWAP/RAM usage, we also display the top 10 processes consuming the most RAM
748
+ out_msg += "\n" + await self.get_ordered_processes(get_non_consuming_processes=False, order_by_ram=True, max_processes=10)
749
+
730
750
  logging.warning(msg=out_msg)
731
751
  elif not display_only_if_critical:
732
752
  if percent_swap >= self.warning_swap_percent:
@@ -1558,7 +1578,7 @@ class LinuxMonitor:
1558
1578
 
1559
1579
  try:
1560
1580
  # Fetch the logs since the specified date
1561
- command: str = f"sudo last --ip --hostlast --time-format iso --since '{past_date}' | grep 'pts/'"
1581
+ command: str = f"sudo last --ip --hostlast --time-format iso --since '{past_date}' | grep 'pts/' || true"
1562
1582
  last_output: str = subprocess.check_output(
1563
1583
  args=command,
1564
1584
  shell=True,
@@ -2134,7 +2154,7 @@ class LinuxMonitor:
2134
2154
  try:
2135
2155
  # Load average
2136
2156
  if is_private:
2137
- msg = self.check_load_average(display_only_if_critical=True)
2157
+ msg = await self.check_load_average(display_only_if_critical=True)
2138
2158
  if msg != "":
2139
2159
  logging.warning(msg=msg)
2140
2160
  if datetime_last_load_average_error_displayed is None or ((datetime.now() - datetime_last_load_average_error_displayed).total_seconds() > self.max_duration_seconds_showing_same_error_again_in_scheduled_tasks):
@@ -2156,7 +2176,7 @@ class LinuxMonitor:
2156
2176
 
2157
2177
  # CPU
2158
2178
  if is_private:
2159
- msg = self.check_cpu_usage(display_only_if_critical=True)
2179
+ msg = await self.check_cpu_usage(display_only_if_critical=True)
2160
2180
  if msg != "":
2161
2181
  logging.warning(msg=msg)
2162
2182
  if datetime_last_cpu_usage_error_displayed is None or ((datetime.now() - datetime_last_cpu_usage_error_displayed).total_seconds() > self.max_duration_seconds_showing_same_error_again_in_scheduled_tasks):
@@ -2178,7 +2198,7 @@ class LinuxMonitor:
2178
2198
 
2179
2199
  # RAM
2180
2200
  if is_private:
2181
- msg = self.check_ram_usage(display_only_if_critical=True)
2201
+ msg = await self.check_ram_usage(display_only_if_critical=True)
2182
2202
  if msg != "":
2183
2203
  logging.warning(msg=msg)
2184
2204
  if datetime_last_ram_usage_error_displayed is None or ((datetime.now() - datetime_last_ram_usage_error_displayed).total_seconds() > self.max_duration_seconds_showing_same_error_again_in_scheduled_tasks):
@@ -2200,7 +2220,7 @@ class LinuxMonitor:
2200
2220
 
2201
2221
  # Swap
2202
2222
  if is_private:
2203
- msg = self.check_swap_usage(display_only_if_critical=True)
2223
+ msg = await self.check_swap_usage(display_only_if_critical=True)
2204
2224
  if msg != "":
2205
2225
  logging.warning(msg=msg)
2206
2226
  if datetime_last_swap_usage_error_displayed is None or ((datetime.now() - datetime_last_swap_usage_error_displayed).total_seconds() > self.max_duration_seconds_showing_same_error_again_in_scheduled_tasks):
@@ -2547,9 +2567,17 @@ class LinuxMonitor:
2547
2567
  out_msg += "\n"
2548
2568
  out_msg += msg
2549
2569
 
2570
+ # Load average
2571
+ if is_private:
2572
+ msg = await self.check_load_average(display_only_if_critical=False)
2573
+ if msg != "":
2574
+ if out_msg != "":
2575
+ out_msg += "\n"
2576
+ out_msg += msg
2577
+
2550
2578
  # CPU
2551
2579
  if is_private:
2552
- msg = self.check_cpu_usage(display_only_if_critical=False)
2580
+ msg = await self.check_cpu_usage(display_only_if_critical=False)
2553
2581
  if msg != "":
2554
2582
  if out_msg != "":
2555
2583
  out_msg += "\n"
@@ -2557,7 +2585,7 @@ class LinuxMonitor:
2557
2585
 
2558
2586
  # RAM
2559
2587
  if is_private:
2560
- msg = self.check_ram_usage(display_only_if_critical=False)
2588
+ msg = await self.check_ram_usage(display_only_if_critical=False)
2561
2589
  if msg != "":
2562
2590
  if out_msg != "":
2563
2591
  out_msg += "\n"
@@ -2565,7 +2593,7 @@ class LinuxMonitor:
2565
2593
 
2566
2594
  # Swap
2567
2595
  if is_private:
2568
- msg = self.check_swap_usage(display_only_if_critical=False)
2596
+ msg = await self.check_swap_usage(display_only_if_critical=False)
2569
2597
  if msg != "":
2570
2598
  if out_msg != "":
2571
2599
  out_msg += "\n"
@@ -6,7 +6,7 @@ with io.open(file='README.md', mode='r', encoding='utf-8') as readme_file:
6
6
 
7
7
  setup(
8
8
  name="LinuxMonitor",
9
- version="1.5.4",
9
+ version="1.5.6",
10
10
  description="Get information and warning status of Linux server like service, port, ping, ssl certificate, disk/folder/cpu/ram/swap usage, ip connection, ... (Python and shell library, Linux ONLY)",
11
11
  long_description=readme,
12
12
  long_description_content_type="text/markdown",
File without changes
File without changes
File without changes