Last active
August 29, 2015 14:27
-
-
Save uchan-nos/29bd38cce5c683bb9439 to your computer and use it in GitHub Desktop.
web application for socat command.
This file contains hidden or 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/python3 | |
| import json | |
| import re | |
| from socket import gethostname | |
| from subprocess import check_output | |
| MEM_INFO_PATTERN = re.compile('^([^ ]+): *([0-9]+) ?([A-Za-z]+)?$') | |
| def main(): | |
| mem_info_output = check_output(['/bin/cat', '/proc/meminfo']) | |
| mem_info_output = mem_info_output.decode('utf-8') | |
| for line in mem_info_output.splitlines(): | |
| m = MEM_INFO_PATTERN.match(line) | |
| if m.group(1) == 'MemFree': | |
| unit = m.group(3) | |
| scale = 1 | |
| if unit == 'kB': | |
| scale = 1024 | |
| else: | |
| raise ValueError('Unknown unit: ' + unit) | |
| mem_free = int(m.group(2)) * scale | |
| result = json.dumps({ | |
| 'hostname': gethostname(), | |
| 'memfree': mem_free | |
| }) | |
| print('HTTP/1.1 200 OK') | |
| print('Content-Type: application/json') | |
| print('Content-Length: {}'.format(len(result) + 1)) | |
| print() | |
| print(result) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
のような感じに実行しておいて
という感じにすると実行できます。