Compare commits

..

4 commits

Author SHA1 Message Date
4b6da2e46b
add flake input 2024-09-11 10:36:16 +05:30
927be66a48
add README 2024-09-11 10:27:14 +05:30
311fd173ec
add service name to notification 2024-09-11 07:57:18 +05:30
1ca8c4eb55
send desktop notification 2024-09-11 07:53:02 +05:30
11 changed files with 51 additions and 2023 deletions

View file

@ -1,19 +0,0 @@
#!/usr/bin/env bash
set -e
if [[ ! -d "/home/user/dev/monitor" ]]; then
echo "Cannot find source directory; Did you move it?"
echo "(Looking for "/home/user/dev/monitor")"
echo 'Cannot force reload with this script - use "direnv reload" manually and then try again'
exit 1
fi
# rebuild the cache forcefully
_nix_direnv_force_reload=1 direnv exec "/home/user/dev/monitor" true
# Update the mtime for .envrc.
# This will cause direnv to reload again - but without re-building.
touch "/home/user/dev/monitor/.envrc"
# Also update the timestamp of whatever profile_rc we have.
# This makes sure that we know we are up to date.
touch -r "/home/user/dev/monitor/.envrc" "/home/user/dev/monitor/.direnv"/*.rc

View file

@ -1 +0,0 @@
/nix/store/5w3dp0m37794iffsbm9vd9f1xmmhda6i-source

View file

@ -1 +0,0 @@
/nix/store/a0jhvi8hbgb016p73a2mpfna699387is-source

View file

@ -1 +0,0 @@
/nix/store/na7sykizsgkzh9i3wc8m8pz5xfqib2rv-source

View file

@ -1 +0,0 @@
/nix/store/yj1wxm9hh8610iyzqnz75kvs6xl8j3my-source

View file

@ -1 +0,0 @@
/nix/store/75fzkiz8z119lqmay567h8x3xqdcrq6k-nix-shell-env

1
.gitignore vendored
View file

@ -1 +1,2 @@
result result
.direnv

39
README.md Normal file
View file

@ -0,0 +1,39 @@
# Monitor
Report website's status
## Usage
### Oneshot
```nix
nix run github:akr2002/monitor
```
### Persistent
Add the following line to your `flake.nix`
```nix
inputs.monitor.url = "github/akr2002:monitor";
```
Add the following to your `home.nix`
```nix
systemd.user.timers.monitor = {
Install.WantedBy = [ "timers.target" ];
Timer = {
OnBootSec = "1m";
OnUnitActiveSec = "1m";
Unit = "monitor.service";
};
};
systemd.user.services.monitor = {
Unit = {
Description = "A script to monitor websites.";
};
Install = {
WantedBy = [ "default.target" ];
};
Service = {
ExecStart = "${inputs.monitor.packages.${pkgs.system}.default}/bin/monitor";
};
};
```
Start:
```bash
systemctl --user start mointor.timer
```

View file

@ -13,6 +13,7 @@
inherit system; inherit system;
}; };
buildInputs = with pkgs; [ buildInputs = with pkgs; [
libnotify
(python3.withPackages(ps: with ps; [ (python3.withPackages(ps: with ps; [
ipython ipython
requests requests

View file

@ -2,15 +2,19 @@
from datetime import datetime from datetime import datetime
import requests import requests
import subprocess
def check_website(url): def check_website(url):
try: try:
response = requests.get(url) response = requests.get(url)
if response.status_code == 200: if response.status_code == 200:
log_status(f"{url} is up and running.") MESSAGE_UP = f"{url} is up and running"
log_status(MESSAGE_UP)
else: else:
log_status(f"{url} is down. Status code: {response.status_code}") MESSAGE_DOWN = f"{url} is down. Status code: {response.status_code}"
notify_user(MESSAGE_DOWN)
log_status(MESSAGE_DOWN)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print(f"Error: {e}") print(f"Error: {e}")
@ -20,6 +24,10 @@ def log_status(message):
log_file.write(f"{datetime.now()} - {message}\n") log_file.write(f"{datetime.now()} - {message}\n")
def notify_user(message):
subprocess.run(["notify-send", "-a", "Monitor", message])
websites = [ websites = [
"https://adityakumar.xyz", "https://adityakumar.xyz",
"https://blog.adityakumar.xyz", "https://blog.adityakumar.xyz",