Created
November 23, 2015 13:12
-
-
Save pkulev/1f3ad493a0f70c601634 to your computer and use it in GitHub Desktop.
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/python | |
import argparse | |
import os | |
import sys | |
from subprocess import check_output, CalledProcessError | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-p", "--pretend", action="store_true", help="Don't change file.") | |
return parser.parse_args() | |
def _get_pid(name): | |
try: | |
return map(int, check_output(["pidof", name]).split()) | |
except CalledProcessError: | |
return [] | |
def check_status(name="java"): | |
pid = _get_pid(name) | |
return "<h2>status: <u>{0}</u></h2>\n".format("running" if pid else "down") | |
def check_uptime(): | |
return "<h2>uptime: {0}</h2>".format(check_output(["uptime"])) | |
def main(): | |
args = parse_args() | |
index_path = "/var/www/mcmon/index.html" | |
checkers = [ | |
check_uptime, | |
check_status] | |
if args.pretend: | |
for checker in checkers: | |
print(checker()) | |
sys.exit(os.EX_OK) | |
with open(index_path, "w") as fd: | |
for checker in checkers: | |
fd.write(checker()) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment