-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPUTemps.py
executable file
·178 lines (138 loc) · 5.02 KB
/
CPUTemps.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
# ========================================================
# Name: CPUTemps.py
# --------------------------------------------------------
# Author:
# HDTodd based on work of prior authors,
# Most recently: Craig Deaton
# --------------------------------------------------------
# Purpose:
# To query and retrieve data from MyPiTemps.db and graph
# for an internval period selected by the user
# --------------------------------------------------------
# Environment:
# Runs as a Python CGI script and connects to a SQLite3 database
# --------------------------------------------------------
# Invocation:
# http://localhost/cgi-bin/CPUTemps.py
# --------------------------------------------------------
# Parameters:
# Form fields can be used to select past number of hours to graph
# --------------------------------------------------------
# Output:
# Generated HTML for title, form data, javascript for chart from Google Charts, and
# summary stats for min, max, avg temps for the selected period. All displayed via browser
# --------------------------------------------------------
# Modifications:
# Version Date Name Description
# ------------------------------------------------------------------------
# 0001 1-Oct-2015 CD Original script from https://github.com/Pyplate/rpi_temp_logger
# 001 11-Oct-2015 CD modified script for this particular environment, pointed
# dbname to proper file name,
#
# ========================================================
import sqlite3
import sys
import cgi
import cgitb
import socket
# global variables
speriod=(15*60)-1
# -----
# 001
# -----
dbname='/var/databases/MyPiTemps.db'
# convert rows from database into a javascript table
def create_table(rows):
chart_table=""
for row in rows[:-1]:
rowstr="['{0}', {1}],\n".format(str(row[0]),str(row[1]))
chart_table+=rowstr
row=rows[-1]
rowstr="['{0}', {1}]\n".format(str(row[0]),str(row[1]))
chart_table+=rowstr
return chart_table
# main function
# This is where the program starts
def main():
cgitb.enable()
# get data from the database for last 24 hours
# if you choose not to use the SQLite3 database, construct you record array in a similar manner to the output
# from the cursor fetch all rows
# date-time string temperature string \n
# and then skip the checking for len(records) and doing the chart_table function, just call the
#
conn=sqlite3.connect(dbname)
curs=conn.cursor()
option = 48
curs.execute("SELECT * FROM PiCoreTemps WHERE DateTime>datetime('now','-%s hours')" % option)
records=curs.fetchall()
conn.close()
# -------------------------------------
# print the HTTP header
# -------------------------------------
print "Content-type: text/html\n\n"
# -------------------------------------
# convert record rows to table if anyreturned
# -------------------------------------
if len(records) != 0:
# convert the data into a table
table=create_table(records)
else:
print "No data found"
sys.stdout.flush()
return
# -------------------------------------
# start printing the page
# -------------------------------------
print "<html>"
# -------------------------------------
# print the HTML head section
# -------------------------------------
print "<head>"
print " <title>"
print "Raspberry Pi CPU Core Temperature Log for host " + socket.gethostname()
print " </title>"
# -------------------------------------
# format and print the graph
# -------------------------------------
# print_graph_script(table)
# google chart snippet
chart_code="""
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Time', 'Temp (C)'],
%s
]);
var options = {
title: 'Temp (C)'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>"""
print chart_code % (table)
print "</head>"
# ---------------------------------------
# print the page body
# ---------------------------------------
print "<body>"
print "<h1>'" + socket.gethostname() + "' Raspberry Pi CPU Core Temperature Log</h1>"
print "<hr>"
# ------------------------------------
# show_graph()
# ------------------------------------
# print the div that contains the graph
print "<h2>CPU Core Temperature Chart</h2>"
print '<div id="chart_div" style="width: 900px; height: 500px;"></div>'
print "<hr>"
print "</body>"
print "</html>"
conn.close()
sys.stdout.flush()
if __name__=="__main__":
main()