-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissioncontrol.py
314 lines (277 loc) · 13.9 KB
/
missioncontrol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import asyncio
import discord
from discord.ext import commands, tasks
import subprocess
import logging
from dotenv import load_dotenv
from datetime import datetime
import locale
import os
import re
import glob
# Logging einrichten
logging.basicConfig(
filename='bot_log.log', # Log-Datei
filemode='a', # Anhängen an die Datei
format='%(asctime)s - %(levelname)s - %(message)s', # Format der Log-Einträge
level=logging.INFO # Mindest-Level: INFO (alternativ DEBUG, ERROR)
)
# Log-Meldung, dass der Bot gestartet wird
logging.info("Starting bot...")
load_dotenv()
DISCORD_BOT_TOKEN = os.getenv('DISCORD_BOT_TOKEN')
DISCORD_CHANNEL_ID = int(os.getenv('DISCORD_CHANNEL_ID'))
SERVICE_CHECK_RATE = int(os.getenv('SERVICE_CHECK_RATE'), 10)
CHANNEL_PRECEDING_CHARACTER = os.getenv('CHANNEL_PRECEDING_CHARACTER', '')
SERVICE_USER = os.getenv('SERVICE_USER')
intents = discord.Intents.default()
intents.message_content = True
intents.messages = True
def get_status_emoji(status_message):
if 'active (running)' in status_message:
return '🟢'
elif 'inactive' in status_message or 'failed' in status_message:
return '🔴'
else:
return '🟡'
def get_docker_status_emoji(status):
if 'Up' in status:
return '🟢'
elif 'Exited' in status or 'Restarting' in status:
return '🔴'
else:
return '🟡'
def get_services_from_systemd():
service_files = glob.glob('/etc/systemd/system/*.service')
services = [os.path.basename(service) for service in service_files]
logging.info(f"Found {len(services)} services from systemd.")
return services
def get_enabled_services_with_user():
services = get_services_from_systemd()
services_with_user = []
for service in services:
service_status = subprocess.run(['systemctl', 'cat', service], capture_output=True, text=True)
if re.search(rf'^User={SERVICE_USER}', service_status.stdout, re.MULTILINE):
services_with_user.append(service)
if not services_with_user:
logging.warning(f"No services found for user: {SERVICE_USER}")
return sorted(services_with_user)
def get_service_status_and_description(service):
result_status = subprocess.run(['systemctl', 'status', service], capture_output=True, text=True)
result_description = subprocess.run(['systemctl', 'cat', service], capture_output=True, text=True)
status = result_status.stdout.splitlines()[:6]
status = "\n".join(status)
description_match = re.search(r'^Description=(.*)', result_description.stdout, re.MULTILINE)
description = description_match.group(1) if description_match else "No description available"
return status, description
def get_docker_containers():
result = subprocess.run(['docker', 'ps', '-a', '--format', '{{.Names}}: {{.Status}}'], capture_output=True, text=True)
containers = result.stdout.splitlines()
logging.info(f"Found {len(containers)} Docker containers.")
return containers
# Docker Control Buttons für Container-Interaktionen
class DockerControlButtons(discord.ui.View):
def __init__(self, container_name, show_start=True, show_stop_restart=True):
super().__init__()
self.container_name = container_name
if show_start:
start_button = discord.ui.Button(label='Start', style=discord.ButtonStyle.green, custom_id=f'start_{container_name}')
start_button.callback = self.start_container
self.add_item(start_button)
if show_stop_restart:
stop_button = discord.ui.Button(label='Stop', style=discord.ButtonStyle.red, custom_id=f'stop_{container_name}')
stop_button.callback = self.stop_container
self.add_item(stop_button)
restart_button = discord.ui.Button(label='Restart', style=discord.ButtonStyle.blurple, custom_id=f'restart_{container_name}')
restart_button.callback = self.restart_container
self.add_item(restart_button)
async def start_container(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
subprocess.run(['docker', 'start', self.container_name])
await asyncio.sleep(5)
logging.info(f"{self.container_name} Docker container started.")
await interaction.followup.send(f'{self.container_name} started.', ephemeral=True)
async def stop_container(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
subprocess.run(['docker', 'stop', self.container_name])
await asyncio.sleep(5)
logging.info(f"{self.container_name} Docker container stopped.")
await interaction.followup.send(f'{self.container_name} stopped.', ephemeral=True)
async def restart_container(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
subprocess.run(['docker', 'restart', self.container_name])
await asyncio.sleep(5)
logging.info(f"{self.container_name} Docker container restarted.")
await interaction.followup.send(f'{self.container_name} restarted.', ephemeral=True)
# Service Control Buttons für systemd Service-Interaktionen
class ServiceControlButtons(discord.ui.View):
def __init__(self, service, show_start=True, show_stop_restart=True):
super().__init__()
self.service = service
if show_start:
start_button = discord.ui.Button(label='Start', style=discord.ButtonStyle.green, custom_id=f'start_{service}')
start_button.callback = self.start_service
self.add_item(start_button)
if show_stop_restart:
stop_button = discord.ui.Button(label='Stop', style=discord.ButtonStyle.red, custom_id=f'stop_{service}')
stop_button.callback = self.stop_service
self.add_item(stop_button)
restart_button = discord.ui.Button(label='Restart', style=discord.ButtonStyle.blurple, custom_id=f'restart_{service}')
restart_button.callback = self.restart_service
self.add_item(restart_button)
async def start_service(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
subprocess.run(['sudo', 'systemctl', 'start', self.service])
await asyncio.sleep(5)
logging.info(f"{self.service} systemd service started.")
await interaction.followup.send(f'{self.service} started.', ephemeral=True)
async def stop_service(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
subprocess.run(['sudo', 'systemctl', 'stop', self.service])
await asyncio.sleep(5)
logging.info(f"{self.service} systemd service stopped.")
await interaction.followup.send(f'{self.service} stopped.', ephemeral=True)
async def restart_service(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
subprocess.run(['sudo', 'systemctl', 'restart', self.service])
await asyncio.sleep(5)
logging.info(f"{self.service} systemd service restarted.")
await interaction.followup.send(f'{self.service} restarted.', ephemeral=True)
# Dropdown-Menü zur Auswahl von Services und Docker-Containern
class ServiceDropdown(discord.ui.Select):
def __init__(self, services, containers):
service_options = [discord.SelectOption(label=f'Service: {service}') for service in services[:25]]
docker_options = [discord.SelectOption(label=f'Docker: {container.split(": ")[0]}') for container in containers[:25]]
super().__init__(placeholder="Select a Service or Docker Container", min_values=1, max_values=1, options=service_options + docker_options)
async def callback(self, interaction: discord.Interaction):
selected_item = self.values[0]
if selected_item.startswith('Docker'):
container_name = selected_item.split('Docker: ')[1]
container_status = next((status for name, status in [c.split(': ', 1) for c in get_docker_containers()] if name == container_name), None)
if container_status:
emoji = get_docker_status_emoji(container_status)
if 'Up' in container_status:
view = DockerControlButtons(container_name, show_start=False)
else:
view = DockerControlButtons(container_name, show_stop_restart=False)
await interaction.response.send_message(f"{emoji} **Docker {container_name}:**\n```{container_status}```", view=view, ephemeral=True)
else:
logging.warning(f"Unexpected format in Docker container status: {selected_item}")
else:
service = selected_item.split('Service: ')[1]
status, description = get_service_status_and_description(service)
emoji = get_status_emoji(status)
if 'active (running)' in status:
view = ServiceControlButtons(service, show_start=False)
else:
view = ServiceControlButtons(service, show_stop_restart=False)
await interaction.response.send_message(f"{emoji} **{service}:** - ({description})\n```{status}```", view=view, ephemeral=True)
bot = commands.Bot(command_prefix='!', intents=intents)
status_message = None
async def clear_channel(channel):
async for message in channel.history(limit=None):
try:
await message.delete()
await asyncio.sleep(60)
except Exception as e:
logging.error(f"Error deleting a message: {e}")
locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')
def format_last_refresh():
now = datetime.now()
formatted_time = now.strftime('%H:%M')
return f"Last Refresh • heute um {formatted_time} Uhr"
@tasks.loop(seconds=SERVICE_CHECK_RATE)
async def update_service_status():
global status_message
services = get_enabled_services_with_user()
docker_containers = get_docker_containers()
status_messages = []
overall_status = '🟢'
status_messages.append("**Service Overview**")
for service in services[:25]:
status, description = get_service_status_and_description(service)
emoji = get_status_emoji(status)
status_messages.append(f"{emoji} **{service}:** - ({description})")
if emoji == '🔴':
overall_status = '🔴'
elif emoji == '🟡' and overall_status != '🔴':
overall_status = '🟡'
status_messages.append("\n**Docker Overview**")
for container in docker_containers:
split_result = container.split(": ", 1)
if len(split_result) == 2:
name, status = split_result
emoji = get_docker_status_emoji(status)
status_messages.append(f"{emoji} **Docker {name}:** - ({status})")
if emoji == '🔴':
overall_status = '🔴'
elif emoji == '🟡' and overall_status != '🔴':
overall_status = '🟡'
else:
logging.warning(f"Unexpected format in Docker container status: {container}")
continue
embed = discord.Embed(title="Status Overview", description="\n".join(status_messages), color=discord.Color.blue())
last_refresh = format_last_refresh()
embed.set_footer(text=last_refresh)
if status_message:
dropdown = ServiceDropdown(services, docker_containers)
view = discord.ui.View()
view.add_item(dropdown)
await status_message.edit(embed=embed, view=view)
channel = status_message.channel
await update_channel_name(channel, overall_status)
async def send_service_status(channel):
global status_message
services = sorted(get_enabled_services_with_user())
docker_containers = get_docker_containers()
if not services and not docker_containers:
await channel.send("No services or Docker containers found.")
return
status_messages = []
overall_status = '🟢'
status_messages.append("**Service Overview**")
for service in services[:25]:
status, description = get_service_status_and_description(service)
emoji = get_status_emoji(status)
status_messages.append(f"{emoji} **{service}:** - ({description})")
if emoji == '🔴':
overall_status = '🔴'
elif emoji == '🟡' and overall_status != '🔴':
overall_status = '🟡'
status_messages.append("\n**Docker Overview**")
for container in docker_containers:
split_result = container.split(": ", 1)
if len(split_result) == 2:
name, status = split_result
emoji = get_docker_status_emoji(status)
status_messages.append(f"{emoji} **Docker {name}:** - ({status})")
if emoji == '🔴':
overall_status = '🔴'
elif emoji == '🟡' and overall_status != '🔴':
overall_status = '🟡'
else:
logging.warning(f"Unexpected format in Docker container status: {container}")
continue
embed = discord.Embed(title="Status Overview", description="\n".join(status_messages), color=discord.Color.blue())
last_refresh = format_last_refresh()
embed.set_footer(text=last_refresh)
dropdown = ServiceDropdown(services, docker_containers)
view = discord.ui.View()
view.add_item(dropdown)
status_message = await channel.send(embed=embed, view=view)
await update_channel_name(channel, overall_status)
update_service_status.start()
async def update_channel_name(channel, overall_status_emoji):
if not CHANNEL_PRECEDING_CHARACTER:
new_name = f'{overall_status_emoji}status'
else:
new_name = f'{CHANNEL_PRECEDING_CHARACTER}{overall_status_emoji}status'
if channel.name != new_name:
await channel.edit(name=new_name)
@bot.event
async def on_ready():
channel = bot.get_channel(DISCORD_CHANNEL_ID)
await clear_channel(channel)
await send_service_status(channel)
logging.info(f'Bot logged in and monitoring services in Channel-ID {DISCORD_CHANNEL_ID}')
bot.run(DISCORD_BOT_TOKEN)