Updated status command and added logs command
This commit is contained in:
parent
90ed6e64f9
commit
db76ea2f8d
@ -2,4 +2,5 @@ TOKEN="YOUR-DISCORD-BOT-TOKEN"
|
|||||||
|
|
||||||
allowed_users = ["discorduserid"]
|
allowed_users = ["discorduserid"]
|
||||||
services = ["services", "you", "want", "to", "monitor"]
|
services = ["services", "you", "want", "to", "monitor"]
|
||||||
command_prefix = ":"
|
command_prefix = ":"
|
||||||
|
logs_dir = "/home/user/logs"
|
65
main.py
65
main.py
@ -1,10 +1,11 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import asyncio
|
import asyncio
|
||||||
import time
|
|
||||||
import os
|
import os
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
|
||||||
import requests
|
|
||||||
import discord
|
import discord
|
||||||
|
import requests
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
import config
|
import config
|
||||||
@ -62,21 +63,38 @@ async def on_ready():
|
|||||||
|
|
||||||
@bot.command(name="status")
|
@bot.command(name="status")
|
||||||
@login_required
|
@login_required
|
||||||
async def status(ctx):
|
async def status(ctx, *args):
|
||||||
"""Renvoie le statut des différents services"""
|
"""Renvoie le statut des différents services"""
|
||||||
embed = discord.Embed(colour=discord.Colour.blue())
|
if len(args) == 0:
|
||||||
embed.set_author(name=time.strftime("%m-%d-%Y %H:%M"))
|
embed = discord.Embed(colour=discord.Colour.blue())
|
||||||
for service in config.services:
|
embed.set_author(name=time.strftime("%m-%d-%Y %H:%M"))
|
||||||
service_status = check_status(service)
|
for service in config.services:
|
||||||
|
service_status = check_status(service)
|
||||||
|
embed.add_field(
|
||||||
|
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(
|
embed.add_field(
|
||||||
name=service, value=service_status, inline=False
|
name=args[0], value=f"```\n{out[:min(len(out)-1, 1017)]}```"
|
||||||
)
|
)
|
||||||
await ctx.reply(embed=embed)
|
await ctx.reply(embed=embed)
|
||||||
|
|
||||||
|
|
||||||
@bot.command(name="service")
|
@bot.command(name="service")
|
||||||
@login_required
|
@login_required
|
||||||
async def status(ctx, *args):
|
async def service(ctx, *args):
|
||||||
"""Exécute une commande systemd"""
|
"""Exécute une commande systemd"""
|
||||||
error = None
|
error = None
|
||||||
commands = ["start", "stop", "enable", "disable", "restart"]
|
commands = ["start", "stop", "enable", "disable", "restart"]
|
||||||
@ -94,4 +112,29 @@ async def status(ctx, *args):
|
|||||||
await ctx.message.add_reaction("🆗")
|
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)
|
bot.run(config.TOKEN)
|
||||||
|
Loading…
Reference in New Issue
Block a user