Skip to content

Commit d7e35b6

Browse files
committed
1.3.0: Add stop service feature
1 parent 071d050 commit d7e35b6

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Displayed infos are not the same if you do the command in private or public chan
4646
- `/restart_service {service_name}`: 🚀 Restart a service 🚀
4747
<img src="https://raw.githubusercontent.com/QuentinCG/Discord-Bot-Linux-Monitor-Python-Library/master/example/restart_service.jpg" height="150">
4848

49+
- `/stop_service {service_name}`: 🚫 Stop a service 🚫
50+
4951
- `/list_services`: 📋 List all available services 📋
5052
<img src="https://raw.githubusercontent.com/QuentinCG/Discord-Bot-Linux-Monitor-Python-Library/master/example/list_services.jpg" height="150">
5153

config-example.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,24 +152,28 @@
152152
{
153153
"web": {
154154
"display_name": "Web server (Apache)",
155-
"restart_command": ["sudo", "/etc/init.d/apache2", "restart"],
156-
"status_command": ["sudo", "/etc/init.d/apache2", "status"],
155+
"restart_command": "sudo /etc/init.d/apache2 restart",
156+
"status_command": "sudo /etc/init.d/apache2 status",
157157
"timeout_in_sec": 30,
158-
"is_private": false
158+
"is_private": false,
159+
"auto_restart": true
159160
},
160161
"sql": {
161162
"display_name": "SQL server (MariaDB)",
162-
"restart_command": ["sudo", "/etc/init.d/mariadb", "restart"],
163-
"status_command": ["sudo", "/etc/init.d/mariadb", "status"],
163+
"restart_command": "sudo /etc/init.d/mariadb restart",
164+
"status_command": "sudo /etc/init.d/mariadb status",
164165
"timeout_in_sec": 30,
165-
"is_private": false
166+
"is_private": false,
167+
"auto_restart": true
166168
},
167169
"rdp": {
168170
"display_name": "RDP (xrdp)",
169-
"restart_command": ["sudo", "systemctl", "restart", "xrdp"],
170-
"status_command": ["sudo", "systemctl", "is-active", "xrdp"],
171+
"restart_command": "sudo systemctl restart xrdp",
172+
"stop_command": "sudo systemctl stop xrdp",
173+
"status_command": "sudo systemctl is-active xrdp",
171174
"timeout_in_sec": 30,
172-
"is_private": true
175+
"is_private": true,
176+
"auto_restart": false
173177
}
174178
},
175179
"certificates": [

discordbotlinuxmonitor/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ async def restart_all(interaction: discord.Interaction) -> None: # type: ignore
9898
async def restart_service(interaction: discord.Interaction, service_name: str) -> None: # type: ignore
9999
await discord_bot_linux_monitor.restart_service(interaction, service_name)
100100

101+
@discord_bot.tree.command(name="stop_service", description="🚫 Stop a service 🚫")
102+
async def stop_service(interaction: discord.Interaction, service_name: str) -> None: # type: ignore
103+
await discord_bot_linux_monitor.stop_service(interaction, service_name)
104+
101105
@discord_bot.tree.command(name="list_services", description="📋 List all available services 📋")
102106
async def list_services(interaction: discord.Interaction) -> None: # type: ignore
103107
await discord_bot_linux_monitor.list_services(interaction)

discordbotlinuxmonitor/discordbotlinuxmonitor.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- Get hostname, OS details, kernel version, server datetime, uptime
2121
- Get connected users
2222
23+
- Restart/Stop a service
24+
2325
- Get processes list (PID and name)
2426
- Kill a process by PID
2527
@@ -31,7 +33,7 @@
3133
__license__ = "MIT License"
3234
__copyright__ = "Copyright Quentin Comte-Gaz (2024)"
3335
__python_version__ = "3.+"
34-
__version__ = "1.2.2 (2024/09/19)"
36+
__version__ = "1.3.0 (2024/09/19)"
3537
__status__ = "Usable for any Linux project"
3638

3739
# pyright: reportMissingTypeStubs=false
@@ -558,7 +560,7 @@ async def websites(self, interaction: discord.Interaction) -> None:
558560

559561
try:
560562
is_private: bool = self._is_private_channel(channel=interaction.channel) # type: ignore
561-
out_msg: str = await self.monitoring.check_all_websites(is_private=is_private, display_only_if_critical=False, restart_if_down=True)
563+
out_msg: str = await self.monitoring.check_all_websites(is_private=is_private, display_only_if_critical=False)
562564

563565
# Respond to the user
564566
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
@@ -661,7 +663,7 @@ async def restart_service(self, interaction: discord.Interaction, service_name:
661663
try:
662664
# Redémarrer le service et récupérer le message de sortie
663665
is_private: bool = self._is_private_channel(channel=interaction.channel) # type: ignore
664-
out_msg: str = await self.monitoring.restart_service(is_private=is_private, service_name=service_name)
666+
out_msg: str = await self.monitoring.restart_service(is_private=is_private, service_name=service_name, force_restart=True)
665667

666668
# Répondre à l'utilisateur
667669
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
@@ -671,6 +673,27 @@ async def restart_service(self, interaction: discord.Interaction, service_name:
671673
logging.exception(msg=out_msg)
672674
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
673675

676+
async def stop_service(self, interaction: discord.Interaction, service_name: str) -> None:
677+
if not self._check_if_valid_guild(guild=interaction.guild):
678+
return
679+
if not (await self._is_bot_channel_interaction(interaction=interaction, send_message_if_not_bot=True)):
680+
return
681+
682+
# Indiquer que la commande est en cours de traitement
683+
await interaction.response.defer()
684+
685+
try:
686+
# Arrêter le service et récupérer le message de sortie
687+
is_private: bool = self._is_private_channel(channel=interaction.channel) # type: ignore
688+
out_msg: str = await self.monitoring.stop_service(is_private=is_private, service_name=service_name)
689+
690+
# Répondre à l'utilisateur
691+
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
692+
except Exception as e:
693+
out_msg = f"**Internal error stopping service {service_name}**:\n```sh\n{e}\n```"
694+
logging.exception(msg=out_msg)
695+
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
696+
674697
async def list_services(self, interaction: discord.Interaction) -> None:
675698
if not self._check_if_valid_guild(guild=interaction.guild):
676699
return
@@ -683,7 +706,7 @@ async def list_services(self, interaction: discord.Interaction) -> None:
683706
try:
684707
# Récupérer la liste des services
685708
is_private: bool = self._is_private_channel(channel=interaction.channel) # type: ignore
686-
out_msg: str = self.monitoring.get_all_services_allowed_to_restart(is_private=is_private)
709+
out_msg: str = self.monitoring.get_all_services(is_private=is_private)
687710

688711
# Répondre à l'utilisateur
689712
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)
@@ -704,7 +727,7 @@ async def ports(self, interaction: discord.Interaction) -> None:
704727
try:
705728
# Récupérer le statut des ports
706729
is_private: bool = self._is_private_channel(channel=interaction.channel) # type: ignore
707-
out_msg: str = await self.monitoring.check_all_ports(is_private=is_private, display_only_if_critical=False, restart_if_down=True)
730+
out_msg: str = await self.monitoring.check_all_ports(is_private=is_private, display_only_if_critical=False)
708731

709732
# Répondre à l'utilisateur
710733
await self._interaction_followup_send_no_limit(interaction=interaction, msg=out_msg)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="DiscordBotLinuxMonitor",
9-
version="1.2.2",
9+
version="1.3.0",
1010
description="From discord channels: 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)",
1111
long_description=readme,
1212
long_description_content_type="text/markdown",
@@ -36,7 +36,7 @@
3636
keywords='discord bot warning info linux monitor monitoring server service port ping ssl certificate disk folder cpu ram swap usage ip connection',
3737
platforms='Linux',
3838
install_requires=[
39-
"linuxmonitor~=1.2.0", # follows the {MAJOR}.{MINOR}.x version range (because LinuxMonitor and DiscordBotLinuxMonitor are tightly coupled and should have same major and minor version)
39+
"linuxmonitor~=1.3.0", # follows the {MAJOR}.{MINOR}.x version range (because LinuxMonitor and DiscordBotLinuxMonitor are tightly coupled and should have same major and minor version)
4040
"discord.py",
4141
"typing",
4242
"asyncio",

0 commit comments

Comments
 (0)