Skip to content

Instantly share code, notes, and snippets.

@beantowel
Created March 22, 2019 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beantowel/5c0909305389579306b74aaa1d4ce88f to your computer and use it in GitHub Desktop.
Save beantowel/5c0909305389579306b74aaa1d4ce88f to your computer and use it in GitHub Desktop.
post system information to gist file
import click
import time
import timeout_decorator
import subprocess
import requests
import datetime
import socket
import re
HOST_NAME = socket.gethostname()
GIT_USER_ID = ''
GIT_TOKEN = ''
GIST_ID = ''
@timeout_decorator.timeout(3, timeout_exception=TimeoutError)
def askIP(url):
output = subprocess.check_output(('curl', url), stderr=subprocess.DEVNULL)
return output.decode('ASCII').strip()
def getPublicIPs():
IP_SERVICES = [
'ident.me',
'myip.dnsomatic.com',
'whatismyip.akamai.com',
'myip.dnsomatic.com',
'ifconfig.me',
'icanhazip.com',
'ipecho.net/plain',
]
for url in IP_SERVICES:
try:
ip = askIP(url)
yield ip
except TimeoutError as e:
print('time out:', url, e)
def getNgrokAddr():
url = 'http://127.0.0.1:4040/status'
r = requests.get(url)
res = re.search('tcp://.*?\\\\', r.text)
addr = res.group()
return addr
def getCPUStatus():
ps = subprocess.Popen(('cat', '/proc/cpuinfo'), stdout=subprocess.PIPE)
output = subprocess.check_output(
('grep', '-E', "model name|Hz"), stdin=ps.stdout)
ps.wait()
return output.decode('ASCII').strip()
def getNv_smi():
try:
output = subprocess.check_output(
('nvidia-smi'), stderr=subprocess.DEVNULL)
return output.decode('ASCII').strip()
except FileNotFoundError:
return 'no nvidia-smi found'
def getUptime():
output_time = subprocess.check_output(('uptime', '-s'))
output_other = subprocess.check_output(('uptime'))
# show uptime in pretty format
s = output_time.decode('ASCII').strip()
s += ','.join(output_other.decode('ASCII').strip().split(',')[1:])
return s
def prepareContent(msg):
# ngrokIP = getNgrokAddr()
# publicIPs = [ip for ip in getPublicIPs()]
now = datetime.datetime.now()
cpuStat = getCPUStatus()
gpuStat = getNv_smi()
upsince = getUptime()
content = \
f"""
posted by {HOST_NAME} @ {now}
up since: {upsince}
msg: {msg}
cpu status:\n{cpuStat}
gpu status:\n{gpuStat}
""".strip()
print(f'content:{content}')
return content
def postToGist(content, fileName='ddns'):
url = 'https://api.github.com/gists/{}'.format(GIST_ID)
payload = {'files': {fileName: {'content': content}}}
header = {'Authorization': 'Bearer {}'.format(GIT_TOKEN)}
print('posting to Gist')
r = requests.patch(url, headers=header, json=payload)
if r.status_code != 200:
print('failed to update Gist')
raise ConnectionError
else:
print('Gist updated')
@click.command()
@click.argument('msg', nargs=1, type=click.STRING)
def sendMsg(msg):
try:
content = prepareContent(msg)
except Exception as e:
content = e
postToGist(content)
if __name__ == "__main__":
sendMsg()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment