Last active
August 11, 2023 22:15
-
-
Save robhaswell/c29b7fb7b9beb0759005c346be869ecb to your computer and use it in GitHub Desktop.
mdadm notifications to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3" | |
services: | |
mdadm-monitor: | |
container_name: mdadm-monitor | |
build: mdadm-monitor | |
privileged: true | |
restart: always | |
volumes: | |
- /proc:/proc | |
- /dev:/dev | |
- ${PWD}/conf/mdadm-monitor/mdadm.conf:/etc/mdadm/mdadm.conf | |
environment: | |
SLACK_URL: https://hooks.slack.com/services/***/***/*** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ubuntu:20.04 | |
RUN apt-get update -y | |
RUN apt-get upgrade -y | |
RUN apt-get install -y mdadm python3-minimal ca-certificates | |
RUN mkdir /app | |
WORKDIR /app | |
ADD notify.py . | |
RUN chmod +x notify.py | |
CMD mdadm --monitor --scan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mdadm.conf | |
# | |
# !NB! Run update-initramfs -u after updating this file. | |
# !NB! This will ensure that initramfs has an uptodate copy. | |
# | |
# Please refer to mdadm.conf(5) for information about this file. | |
# | |
# by default (built-in), scan all partitions (/proc/partitions) and all | |
# containers for MD superblocks. alternatively, specify devices to scan, using | |
# wildcards if desired. | |
#DEVICE partitions containers | |
# automatically tag new arrays as belonging to the local system | |
HOMEHOST <system> | |
# instruct the monitoring daemon how to send alerts | |
PROGRAM /app/notify.py | |
# definitions of existing MD arrays | |
# This configuration was auto-generated on Sun, 21 Apr 2019 15:54:55 +0100 by mkconf | |
ARRAY /dev/md/127_0 metadata=0.90 UUID=8167a3b7:fc362825:409a2900:8a49c38d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Send notifications of mdadm events to Slack. | |
""" | |
import argparse | |
import json | |
import os | |
import subprocess | |
import sys | |
import urllib.request | |
SLACK_URL = os.environ['SLACK_URL'] | |
def main(): | |
""" | |
Main program. | |
""" | |
parser = argparse.ArgumentParser() | |
parser.add_argument("event", help="Event") | |
parser.add_argument("device", help="Device") | |
parser.add_argument("component", help="Related component device", nargs="?") | |
args = parser.parse_args() | |
# Get mdadm detail | |
proc = subprocess.run(["mdadm", "--detail", args.device], | |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True) | |
detail = proc.stdout.decode("utf8") | |
# Notify slack | |
data = { | |
"attachments": [ | |
{ | |
"color": '#FF0000', | |
"text": (f"❌ *mdadm: {args.event}* on *{args.device}*\n" | |
f"```{detail}```"), | |
}, | |
], | |
} | |
headers = { | |
"Content-type": "application/json" | |
} | |
req = urllib.request.Request(SLACK_URL, data=json.dumps(data).encode("utf8"), headers=headers) | |
with urllib.request.urlopen(req) as resp: | |
pass | |
if resp.status != 200: | |
print(resp.status) | |
sys.exit(1) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment