Updated status command and added logs command
This commit is contained in:
parent
90ed6e64f9
commit
db76ea2f8d
@ -3,3 +3,4 @@ TOKEN="YOUR-DISCORD-BOT-TOKEN"
|
||||
allowed_users = ["discorduserid"]
|
||||
services = ["services", "you", "want", "to", "monitor"]
|
||||
command_prefix = ":"
|
||||
logs_dir = "/home/user/logs"
|
51
main.py
51
main.py
@ -1,10 +1,11 @@
|
||||
#!/usr/bin/python3
|
||||
import asyncio
|
||||
import time
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
import requests
|
||||
import discord
|
||||
import requests
|
||||
from discord.ext import commands
|
||||
|
||||
import config
|
||||
@ -62,8 +63,9 @@ async def on_ready():
|
||||
|
||||
@bot.command(name="status")
|
||||
@login_required
|
||||
async def status(ctx):
|
||||
async def status(ctx, *args):
|
||||
"""Renvoie le statut des différents services"""
|
||||
if len(args) == 0:
|
||||
embed = discord.Embed(colour=discord.Colour.blue())
|
||||
embed.set_author(name=time.strftime("%m-%d-%Y %H:%M"))
|
||||
for service in config.services:
|
||||
@ -72,11 +74,27 @@ async def status(ctx):
|
||||
name=service, value=service_status, inline=False
|
||||
)
|
||||
await ctx.reply(embed=embed)
|
||||
else:
|
||||
if args[0] not in config.services:
|
||||
await ctx.reply(f"Service inconnu `{args[0]}`")
|
||||
return None
|
||||
try:
|
||||
out = subprocess.check_output(["systemctl", "status", args[0]], stderr=subprocess.STDOUT)
|
||||
except Exception as e:
|
||||
out = e.output
|
||||
|
||||
out = out.decode("UTF-8")
|
||||
embed = discord.Embed(colour=discord.Colour.blue())
|
||||
embed.set_author(name=time.strftime("%m-%d-%Y %H:%M"))
|
||||
embed.add_field(
|
||||
name=args[0], value=f"```\n{out[:min(len(out)-1, 1017)]}```"
|
||||
)
|
||||
await ctx.reply(embed=embed)
|
||||
|
||||
|
||||
@bot.command(name="service")
|
||||
@login_required
|
||||
async def status(ctx, *args):
|
||||
async def service(ctx, *args):
|
||||
"""Exécute une commande systemd"""
|
||||
error = None
|
||||
commands = ["start", "stop", "enable", "disable", "restart"]
|
||||
@ -94,4 +112,29 @@ async def status(ctx, *args):
|
||||
await ctx.message.add_reaction("🆗")
|
||||
|
||||
|
||||
@bot.command(name="logs")
|
||||
@login_required
|
||||
async def logs(ctx, *args):
|
||||
"""Renvoie des fichier logs"""
|
||||
logs_files = [
|
||||
i[:-4] for i in os.listdir(config.logs_dir) if i.endswith(".txt") and os.path.isfile(os.path.join(config.logs_dir, i))
|
||||
]
|
||||
|
||||
if len(args) == 0:
|
||||
embed = discord.Embed(colour=discord.Colour.blue())
|
||||
embed.set_author(name=time.strftime("%m-%d-%Y %H:%M"))
|
||||
embed.add_field(
|
||||
name="Fichiers de logs disponibles", value="\n".join(logs_files)
|
||||
)
|
||||
await ctx.reply(embed=embed)
|
||||
return None
|
||||
|
||||
if args[0] not in logs_files:
|
||||
print(logs_files)
|
||||
await ctx.reply("Ce fichier n'existe pas.")
|
||||
return None
|
||||
|
||||
with open(os.path.join(config.logs_dir, f"{args[0]}.txt"), "r") as fp:
|
||||
await ctx.reply(file=discord.File(fp, filename=f"{args[0]}.txt"))
|
||||
|
||||
bot.run(config.TOKEN)
|
||||
|
Loading…
Reference in New Issue
Block a user