From 8279bdb2ea3196cc068ab607f40d1954cf395cf1 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 30 Apr 2019 15:48:55 +0200 Subject: [PATCH] add example to show system status --- examples/README.md | 8 ++- examples/send-system-status/botacl | 17 +++++ examples/send-system-status/mycommands.sh | 84 +++++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 examples/send-system-status/botacl create mode 100644 examples/send-system-status/mycommands.sh diff --git a/examples/README.md b/examples/README.md index 24535a1..d1189f6 100644 --- a/examples/README.md +++ b/examples/README.md @@ -28,10 +28,16 @@ Background jobs are an easy way to provide sceduled messages or alerts if someth **Note:** Output of system commands often contains newlines, each newline results in a telegram message, the function 'send_telegram' in mycommands.sh avoids this by converting each newline to ' mynewlinestartshere ' before output the string. +**system-status** contains an example with commands showing system status. This example is adapted from https://github.com/RG72/telegram-bot-bash + +``` + mycommands.sh - sommands to show system status + botacl - controls who can show system status +``` ### Use bashbot from external scripts **external-use** will contain some examples on how to send messages from external scripts to Telegram chats or users. -#### $$VERSION$$ v0.7-pre2-3-ge0a6d38 +#### $$VERSION$$ v0.7-pre2-4-g8dcbc29 diff --git a/examples/send-system-status/botacl b/examples/send-system-status/botacl new file mode 100644 index 0000000..40a251d --- /dev/null +++ b/examples/send-system-status/botacl @@ -0,0 +1,17 @@ +# file: botacl +# a user not listed here, will return false from 'user_is_allowed' +# +# Format: +# user:ressource:chat + +# allow user 123456789 access to all resources in all chats +123456789:*:* + +# allow user 12131415 to request systemstatus in all chats +12131415:systemstatus:* + +# * are only allowed on the right hand side and not for user! +# the following exaples are NOT valid! +*:*:* +*:start:* +*:*:98979695 diff --git a/examples/send-system-status/mycommands.sh b/examples/send-system-status/mycommands.sh new file mode 100644 index 0000000..de64d56 --- /dev/null +++ b/examples/send-system-status/mycommands.sh @@ -0,0 +1,84 @@ +#!/bin/bash +# files: mycommands.sh +# +# this example is rendered after https://github.com/RG72/telegram-bot-bash +# to show how you can customize bashbot by only editing mycommands.sh +# NOTE: this is not tested, simply copied from original source and reworked! +# +#### $$VERSION$$ v0.7-pre2-4-g8dcbc29 +# +# shellcheck disable=SC2154 +# shellcheck disable=SC2034 + + +# uncomment the following lines to overwrite info and help messages +#' + +# returned messages + +bashbot_info='This bot allows you to request status of your system. +To begin using the bot, try with the /help command. +' +bashbot_help='*Availiable commands*: +/s *sensors* +/ss *smbstatus* +/free *memory status* +/md *raid status* +/lvs *lvm status* +/lvsd *Datailed lvm status* +/df *disk space* +/ifconfig *ifconfig output* +/smart *-d sda* _smart status for sda drive_ +' +IDMESSAGEPRE="Your chat identifier is" + + +# your additional bahsbot commands +# NOTE: command can have @botname attached, you must add * in case tests... +mycommands() { + if user_is_allowed "${USER[ID]}" "systemstatus"; then + case "$MESSAGE" in + '/md'|'raid_status') msg="$(cat /proc/mdstat)";; + '/ss'|'smbstatus') + msg="" + smbstatus >/tmp/smbstatus + send_doc "$TARGET" "/tmp/smbstatus" + prevActiveTime=$curTime + ;; + '/s'|'sensors'|'/sensors') msg="$(sensors | sed -r 's/\s|\)+//g' | sed -r 's/\(high=|\(min=/\//' | sed -r 's/\,crit=|\,max=/\//')";; + '/free') msg="$(free -h)";; + '/pvs') msg="$(pvs)";; + '/ifconfig') msg="$(ifconfig)";; + '/vgs') msg="$(vgs)";; + '/lvs') msg="$(lvs | sed -r 's/\s+/\n/g')";; + '/lvsd') msg="$(lvs -a -o +devices | sed -r 's/\s+/\n/g')";; + '/smart'|'/smartctl') + if [ "$OPTARG" == "" ]; then + msg="example \`/smart sda\`" + else + drive="$(echo "$OPTARG" | cut -c 1-3)" + echo "smartctl -a /dev/$drive" + msg="$(smartctl -a "/dev/$drive")" + fi + ;; + '/df') msg="$(df -h | sed -r 's/^/\n/' | sed -r 's/\s+/\n/g')";; + esac + + if [ "$msg" != "" ]; then + send_telegram "${CHAT[ID]}" "$msg" + fi + else + send_normal_message "Sorry, you are not allowed to use this bot!" + fi +} + +# place your processing functions here + +# converts newlines for telegram and send as one message +# $1 chat +# $2 message +send_telegram() { + # output to telegram + send_message "${1}" "$(sed <<< "${2}" -e ':a;N;$!ba;s/\n/ mynewlinestartshere /g')" +} # 2>>"$0.log" +