Skip to content

Commit

Permalink
v3.1.0
Browse files Browse the repository at this point in the history
- ACKNOWLEDGE function added
- Bug fix concerning the log flooding
  • Loading branch information
deexno authored Sep 5, 2023
1 parent 90c88fd commit 11cb853
Showing 1 changed file with 108 additions and 27 deletions.
135 changes: 108 additions & 27 deletions resources/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ async def reschedule_check(
# Execute the check via the CMK CLI and save the output in a variable
# to output it to the user afterwards.
check_result = subprocess.run(
[f"/omd/sites/{omd_site}/bin/cmk", "--check", hostname],
[os.path.join(omd_site_dir, "bin", "cmk"), "--check", hostname],
stdout=subprocess.PIPE,
)

Expand Down Expand Up @@ -863,7 +863,6 @@ async def get_host_problems(
async def get_service_problems(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> int:

try:
# Get list of services from livestatus connection and sort
# by description
Expand Down Expand Up @@ -1253,39 +1252,50 @@ async def send_automatic_notification(context: ContextTypes.DEFAULT_TYPE):
for recipient in recipient_list:
if recipient.isnumeric():
reply_markup = [
InlineKeyboardButton(
"🔂 RECHECK",
callback_data=f"recheck,{description},{hostname},0",
)
[
InlineKeyboardButton(
"🔂 RECHECK",
callback_data=f"recheck,{description},{hostname},0",
)
]
]

if description != "":
reply_markup.append(
InlineKeyboardButton(
"📉 GRAPHS",
callback_data=f"graph,{description},{hostname}",
),
)

reply_markup.append(
InlineKeyboardButton(
"🆘 HELP",
callback_data="help,"
f"hostname:{hostname};"
f"service:{description};"
f"from_state:{from_state};"
f"to_state:{to_state};"
f"output:{output}",
),
)
reply_markup = [
[
InlineKeyboardButton(
"🔂 RECHECK",
callback_data=f"recheck,{description},{hostname},0",
),
InlineKeyboardButton(
"📉 GRAPHS",
callback_data=f"graph,{description},{hostname}",
),
InlineKeyboardButton(
"🆘 HELP",
callback_data="help,"
f"hostname:{hostname};"
f"service:{description};"
f"from_state:{from_state};"
f"to_state:{to_state};"
f"output:{output}",
),
],
[
InlineKeyboardButton(
"✔️ ACKNOWLEDGE",
callback_data=f"ack,{description},{hostname}",
)
],
]

await context.bot.send_message(
chat_id=recipient,
disable_notification=True
if type == "notifications_silent"
else False,
text=message,
reply_markup=InlineKeyboardMarkup([reply_markup]),
reply_markup=InlineKeyboardMarkup(reply_markup),
parse_mode="HTML",
)

Expand Down Expand Up @@ -1647,7 +1657,7 @@ async def get_omd_status(
try:
# Execute the check via the OMD CLI
check_result = subprocess.run(
[f"/omd/sites/{omd_site}/bin/omd", "status"],
[os.path.join(omd_site_dir, "bin", "omd"), "status"],
stdout=subprocess.PIPE,
)

Expand Down Expand Up @@ -1686,7 +1696,7 @@ async def start_omd_services(

# Execute the start command via the OMD CLI
check_result = subprocess.run(
[f"/omd/sites/{omd_site}/bin/omd", "start"],
[os.path.join(omd_site_dir, "bin", "omd"), "start"],
stdout=subprocess.PIPE,
)

Expand Down Expand Up @@ -2009,6 +2019,72 @@ async def ask_question(
)


async def acknowledge_service_problem(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
# Check if the user is authenticated to use the bot
if is_user_authenticated(update.effective_user.id):
query = update.callback_query
await query.answer()
type, description, hostname = query.data.split(",")

now = int(time.time())
nagios_cmd = os.path.join(omd_site_dir, "tmp", "run", "nagios.cmd")
user = update.effective_user
username = user.username

comment = (
"ACKNOWLEDGE_SVC_PROBLEM;"
f"{hostname};"
f"{description};"
"2;"
"0;"
"0;"
f"{username};"
"The problem was acknowledged via the Telegram bot by "
f"{username} ({user.id})."
)

with open(nagios_cmd, "w") as f:
f.write(f"[{now}] {comment}\n")

try:
await context.bot.send_message(
text=translate(
"The service was acknowledged:\n\n"
f"HOST: {hostname}\n"
f"SERVICE: {description}\n"
"STICKY: YES\n"
"NOTIFY OTHERS: YES\n"
"PERSISTEN: NO\n"
),
chat_id=update.effective_user.id,
disable_notification=True,
parse_mode="HTML",
)

bot_handler_job_queue.run_once(
message_all_users,
0,
data=translate(
f"@{username} has acknowledged ✅ the service "
f"'{description}' for the host '{hostname}'"
),
)

except Exception as e:
# If an error occurs, notify the user
logger.critical(e)
await context.bot.send_message(
text=translate(
"I'm sorry but while I was processing your request an "
"error occurred!"
),
chat_id=update.effective_user.id,
reply_markup=home_menu,
)


def main() -> None:
bot_handler_job_queue.run_once(
message_all_users, 0, data=translate("I'm BACK! 🤖")
Expand Down Expand Up @@ -2446,6 +2522,11 @@ def main() -> None:
CallbackQueryHandler(get_ai_help, pattern="^help,")
)

# Add callback handler for "✔️ ACKNOWLEDGE" button
bot_handler.add_handler(
CallbackQueryHandler(acknowledge_service_problem, pattern="^ack,")
)

# Start polling for updates
bot_handler.run_polling()

Expand Down

0 comments on commit 11cb853

Please sign in to comment.