Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
km3mon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
km3py
km3mon
Commits
0888e430
Verified
Commit
0888e430
authored
10 months ago
by
Tamas Gal
Browse files
Options
Downloads
Patches
Plain Diff
Improve chatbot logging and error handling
parent
0667c6b3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Pipeline
#51670
passed
10 months ago
Stage: docker
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/scripts/chatbot.py
+27
-5
27 additions, 5 deletions
backend/scripts/chatbot.py
with
27 additions
and
5 deletions
backend/scripts/chatbot.py
+
27
−
5
View file @
0888e430
...
...
@@ -30,6 +30,7 @@ CONFIG = "pipeline.toml"
RECONNECT_INTERVAL
=
30
with
open
(
CONFIG
,
'
r
'
)
as
fobj
:
print
(
f
"
Reading configuration from
{
CONFIG
}
"
)
config
=
toml
.
load
(
fobj
)
BOTNAME
=
config
[
'
Alerts
'
][
'
botname
'
]
PASSWORD
=
config
[
'
Alerts
'
][
'
password
'
]
...
...
@@ -37,6 +38,7 @@ with open(CONFIG, 'r') as fobj:
def
get_channel_id
(
channel
):
print
(
f
"
Getting channel ID for channel:
{
channel
}
"
)
tries
=
0
while
True
:
tries
+=
1
...
...
@@ -44,9 +46,9 @@ def get_channel_id(channel):
try
:
rocket
=
RocketChat
(
BOTNAME
,
PASSWORD
,
server_url
=
URL
)
except
requests
.
exceptions
.
ConnectionError
as
e
:
log
.
error
(
"
Unable to connect to the RocketChat server:
%s
"
,
e
)
log
.
error
(
f
"
Unable to connect to the RocketChat server:
{
e
}
"
)
except
Exception
as
e
:
log
.
error
(
"
Unknown error occured:
%s
"
,
e
)
log
.
error
(
f
"
Unknown error occured:
{
e
}
"
)
else
:
break
...
...
@@ -54,9 +56,11 @@ def get_channel_id(channel):
print
(
f
"
Reconnecting in
{
interval
}
seconds...
"
)
time
.
sleep
(
interval
)
channels
=
rocket
.
channels_list
().
json
()[
'
channels
'
]
channels
=
rocket
.
channels_list
(
count
=
0
).
json
()[
'
channels
'
]
for
c
in
channels
:
print
(
f
"
->
{
c
[
'
name
'
]
}
=>
{
c
[
'
_id
'
]
}
"
)
if
c
[
'
name
'
]
==
channel
:
print
(
f
"
Found channel ID for
{
channel
}
is
{
c
[
'
_id
'
]
}
"
)
return
c
[
'
_id
'
]
...
...
@@ -64,28 +68,42 @@ CHANNEL_ID = get_channel_id(CHANNEL)
def
run
():
print
(
"
Running the monitoring bot system
"
)
bot
=
spawn_bot
()
register_handlers
(
bot
)
bot
.
run
()
def
spawn_bot
():
print
(
"
Spawning the bot
"
)
return
RocketChatBot
(
BOTNAME
,
PASSWORD
,
URL
)
def
is_shifter
(
user
):
print
(
f
"
Checking if
{
user
}
is a shifter
"
)
with
open
(
CONFIG
,
'
r
'
)
as
fobj
:
config
=
toml
.
load
(
fobj
)
return
user
in
config
[
'
Alerts
'
][
'
shifters
'
]
try
:
alerts_config
=
config
[
'
Alerts
'
]
except
KeyError
:
log
.
error
(
"
No
'
Alerts
'
section found in the configuration file
"
)
return
False
try
:
return
user
in
alerts_config
[
'
shifters
'
]
except
KeyError
:
log
.
error
(
"
No
'
shifters
'
section found in
'
Alerts
'
of the configuration file
"
)
return
False
def
is_operator
(
user
):
print
(
f
"
Checking if
{
user
}
is an operator
"
)
with
open
(
CONFIG
,
'
r
'
)
as
fobj
:
config
=
toml
.
load
(
fobj
)
return
user
in
config
[
'
Alerts
'
][
'
operators
'
]
def
register_handlers
(
bot
):
print
(
"
Registering API handlers
"
)
def
greet
(
msg
,
user
,
channel_id
):
if
channel_id
!=
CHANNEL_ID
:
print
(
"
skipping
"
)
...
...
@@ -93,19 +111,22 @@ def register_handlers(bot):
bot
.
send_message
(
'
hello @
'
+
user
,
channel_id
)
def
status
(
msg
,
user
,
channel_id
):
print
(
f
"
Reporting status to channel
{
channel_id
}
"
)
if
channel_id
!=
CHANNEL_ID
:
print
(
"
s
kipping
"
)
print
(
f
"
S
kipping
channel with ID
{
channel_id
}
"
)
return
if
not
is_shifter
(
user
)
and
not
is_operator
(
user
):
bot
.
send_message
(
"
Sorry @{}, only operators and shifters are allowed to mess
"
"
with me, sorry...
"
.
format
(
user
),
channel_id
)
return
print
(
"
Asking subservisorctl for the status
"
)
try
:
status
=
"
```
\n
"
+
subprocess
.
check_output
(
[
'
supervisorctl
'
,
'
status
'
]).
decode
(
'
ascii
'
)
+
"
\n
```
"
except
subprocess
.
CalledProcessError
as
e
:
status
=
"
```
\n
{}
\n
```
"
.
format
(
e
.
output
.
decode
(
'
ascii
'
))
print
(
"
Sending status
"
)
bot
.
send_message
(
status
,
channel_id
)
def
supervisorctl
(
msg
,
user
,
channel_id
):
...
...
@@ -172,6 +193,7 @@ def register_handlers(bot):
([
'
supervisorctl
'
],
supervisorctl
)]
for
trigger
,
handler
in
handlers
:
bot
.
add_dm_handler
(
trigger
,
handler
)
print
(
"
All handlers are registered
"
)
def
main
():
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment