add narodmon cgi script
This commit is contained in:
parent
e4720e31d3
commit
e1f4bca6c8
@ -94,6 +94,10 @@ with lib; {
|
||||
});
|
||||
|
||||
grub2 = prev.callPackage ./packages/grub { };
|
||||
|
||||
narodmon-py = self.writers.writePython3Bin "temp.py" {
|
||||
libraries = with self.python3Packages; [ requests ];
|
||||
} ./packages/narodmon-py.nix;
|
||||
}
|
||||
)
|
||||
];
|
||||
|
85
profiles/packages/narodmon-py.nix
Normal file
85
profiles/packages/narodmon-py.nix
Normal file
@ -0,0 +1,85 @@
|
||||
''
|
||||
import requests
|
||||
import json
|
||||
import hashlib
|
||||
import uuid
|
||||
import time
|
||||
import os
|
||||
|
||||
app_id = str(uuid.getnode()).encode()
|
||||
md5_app_id = hashlib.md5(app_id).hexdigest()
|
||||
data = {
|
||||
'cmd': 'sensorsValues',
|
||||
'uuid': md5_app_id,
|
||||
'sensors': '81392',
|
||||
'lang': 'ru'
|
||||
}
|
||||
headers = {
|
||||
'User-Agent': 'ataraxiadev.com',
|
||||
'Accept-Encoding': 'gzip, deflate'
|
||||
}
|
||||
|
||||
|
||||
def read_key(filename):
|
||||
with open(filename, 'r', encoding='utf-8') as file:
|
||||
return file.readline().split()[0]
|
||||
|
||||
|
||||
def read_temp(filename):
|
||||
try:
|
||||
file = open(filename, 'r', encoding='utf-8')
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
else:
|
||||
read_time = int(file.readline()) // 1000000000
|
||||
current_time = time.time_ns()
|
||||
# 10 minutes
|
||||
if (current_time - read_time < 600):
|
||||
return None
|
||||
else:
|
||||
return file.readline().split()[0]
|
||||
|
||||
|
||||
def write_temp(filename, temp):
|
||||
with open(filename, 'w', encoding='utf-8') as file:
|
||||
timestamp = time.time_ns()
|
||||
print(timestamp, file=file)
|
||||
print(temp, file=file)
|
||||
|
||||
|
||||
def print_page(text, **args):
|
||||
print('Content-Type: text/plain')
|
||||
print("")
|
||||
if args:
|
||||
print(text, args)
|
||||
else:
|
||||
print(text)
|
||||
|
||||
|
||||
try:
|
||||
tmpdir = os.getenv('TMP') or os.getenv('TMPDIR') or '/tmp'
|
||||
temp_file = os.path.join(tmpdir, "narodmon-temp")
|
||||
temp = read_temp(temp_file)
|
||||
if temp is not None:
|
||||
print_page(temp)
|
||||
raise SystemExit(0)
|
||||
api_key = read_key('/var/secrets/narodmon-key')
|
||||
data['api_key'] = api_key
|
||||
response = requests.post(
|
||||
'http://narodmon.com/api',
|
||||
json=data,
|
||||
headers=headers
|
||||
)
|
||||
result = json.loads(response.text)
|
||||
temp = result['sensors'][0]['value']
|
||||
print_page(temp)
|
||||
write_temp(temp_file, temp)
|
||||
except requests.RequestException as e:
|
||||
print_page('Request error:', e)
|
||||
except (ValueError, TypeError) as e:
|
||||
print_page('JSON error:', e)
|
||||
except (FileNotFoundError, OSError, IOError) as e:
|
||||
print_page("I/O error({0}): {1}".format(e.errno, e.strerror))
|
||||
except Exception as e:
|
||||
print_page("Unexpected error({0}): {1}".format(e.errno, e.strerror))
|
||||
''
|
@ -41,6 +41,12 @@
|
||||
};
|
||||
};
|
||||
|
||||
services.fcgiwrap = {
|
||||
enable = true;
|
||||
user = config.services.nginx.user;
|
||||
group = config.services.nginx.group;
|
||||
};
|
||||
|
||||
services.nginx = {
|
||||
enable = true;
|
||||
group = "acme";
|
||||
@ -87,16 +93,6 @@
|
||||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
'';
|
||||
};
|
||||
locations."/cgi-bin/" = with config.services; {
|
||||
extraConfig = ''
|
||||
gzip off;
|
||||
root /srv/http/ataraxiadev.com;
|
||||
fastcgi_pass ${fcgiwrap.socketType}:${fcgiwrap.socketAddress};
|
||||
include ${pkgs.nginx}/conf/fastcgi_params;
|
||||
include ${pkgs.nginx}/conf/fastcgi.conf;
|
||||
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
||||
'';
|
||||
};
|
||||
} // default;
|
||||
"matrix:443" = {
|
||||
serverAliases = [
|
||||
@ -221,17 +217,25 @@
|
||||
extraConfig = proxySettings;
|
||||
};
|
||||
} // default;
|
||||
"api.ataraxiadev.com" = {
|
||||
locations."~ (\\.py|\\.sh)$" = with config.services; {
|
||||
alias = "/srv/http/api.ataraxiadev.com";
|
||||
extraConfig = ''
|
||||
gzip off;
|
||||
fastcgi_pass ${fcgiwrap.socketType}:${fcgiwrap.socketAddress};
|
||||
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
||||
include ${pkgs.nginx}/conf/fastcgi_params;
|
||||
'';
|
||||
};
|
||||
} // default;
|
||||
};
|
||||
};
|
||||
|
||||
services.fcgiwrap = {
|
||||
enable = true;
|
||||
user = config.services.nginx.user;
|
||||
group = config.services.nginx.group;
|
||||
};
|
||||
secrets.narodmon-key.owner = config.services.nginx.user;
|
||||
|
||||
# Temp.py script
|
||||
environment.systemPackages = with pkgs; [ python3 python3Packages.requests ];
|
||||
system.activationScripts.linkPyScripts.text = ''
|
||||
ln -sfn ${pkgs.narodmon-py}/bin/temp.py /srv/http/api.ataraxiadev.com/temp.py
|
||||
'';
|
||||
|
||||
networking.firewall.allowedTCPPorts = [ 80 443 8448 ];
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user