-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgogl-drive.py
executable file
·70 lines (57 loc) · 2.09 KB
/
gogl-drive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import os
import subprocess
import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
def authorize_client():
json_key = json.load(open(os.path.dirname(os.path.realpath(__file__)) + '/credentials.json', 'r'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
auth = gspread.authorize(credentials)
print('Completed authorization.')
return auth
def shell_cmd(cmd):
"""Cmd should be the command string to execute. The resulting output is returned.
"""
proc = subprocess.Popen([cmd], stdout=subprocess.PIPE, shell=True)
result = proc.stdout.read().replace('\n', '')
return result
def disk_space():
return shell_cmd('df -h | head -n2 | tail -n1 | cut -c30- | cut -d" " -f1')
def find_next_row(worksheet):
""" Finds the first empty row of the worksheet.
"""
val_list = worksheet.col_values(1)
row_num = 1
for row in val_list:
if row == '':
return row_num
row_num += 1
return None
def write_entry(worksheet):
""" Trys to log the next entry in the worksheet.
"""
row = find_next_row(worksheet)
if row is None:
print('Out of rows. Adding 100 more.')
worksheet.add_rows(100)
row = find_next_row(worksheet)
print('Writing data to row ' + str(row) + '.')
row_cells = worksheet.range('A' + str(row) + ':C' + str(row))
row_cells[0].value = shell_cmd('date')
row_cells[1].value = disk_space()
row_cells[2].value = 'test'
worksheet.update_cells(row_cells)
print('Row written.')
return True
#line = shell_cmd('date') + ',' + disk_space() + ',' + shell_cmd('uptime | cut -d" " -f11')
#print(line)
gc = authorize_client()
spread = gc.open_by_key('1k8ZSXOlcGadSFfU8uJLzDMi6Ksr88ZBP6hZe0MWM-18')
wks = spread.sheet1
#print('1min: ' + shell_cmd('uptime | cut -d" " -f10'))
#print('5min: ' + shell_cmd('uptime | cut -d" " -f11'))
#print('15min: ' + shell_cmd('uptime | cut -d" " -f12'))
write_entry(wks)
print('Done')