Updated 2018-08-24 15:04:41 by pooryorick

Created by CecilWesterhof.

I had some problems with the temperature of my CPU. So I wrote a systemd service (with Tcl) to log the temperature every minute to a SQLite database. And wrote the following script to display statistics of the last seven days and the last day:
#!/usr/bin/env tclsh

package require sqlite3


# for getDB: my way to open the db database
source /usr/local/share/tcltk/utilities.tcl


set periodFormat     " %10s | %4s | %4s | %7s | %5s"
set periodDivider    [string map {" " - | +} [format $periodFormat "" "" "" "" ""]]
set periodSelect     {
    SELECT   *
    FROM     temperatureStatistics
    WHERE    date BETWEEN
                      DATE('now', '-7 days')
                  AND DATE('now', '-1 day')
    ORDER BY date DESC
}
set yesterdayFormat  " %11s | %5s"
set yesterdayDivider [string map {" " - | +} [format $yesterdayFormat "" ""]]
set yesterdaySelect  {
    SELECT   Temperature
    ,        COUNT(*)     AS Count
    FROM     temperature
    WHERE    date = DATE('now', '-1 day')
    GROUP BY Temperature
    ORDER BY Temperature DESC
}


getDB
puts $periodDivider
puts [format $periodFormat "Date   " Min Max Average Total]
puts $periodDivider
db eval $periodSelect {
    puts [format $periodFormat $Date $Minimum $Maximum $Average $Count]
}
puts $periodDivider
puts ""
set  i        0
set  subtotal 0
set  total    0
puts $yesterdayDivider
puts [format $yesterdayFormat "Temperature" Total]
puts $yesterdayDivider
db eval $yesterdaySelect {
    puts [format $yesterdayFormat $Temperature $Count]
    incr i
    incr subtotal $Count
    incr total    $Count
    if {[expr $i % 5] == 0} {
        puts $yesterdayDivider
        puts [format $yesterdayFormat "SubTotal" $subtotal]
        puts $yesterdayDivider
        set  subtotal 0
    }
}
if {[expr $i % 5] != 0} {
    puts $yesterdayDivider
    if {[expr $i % 5] > 1} {
        puts [format $yesterdayFormat "SubTotal" $subtotal]
        puts $yesterdayDivider
        set  subtotal 0
    }
}
puts [format $yesterdayFormat "Total" $total]
puts $yesterdayDivider

I put this in a cronjob and the results of this morning where: `
    ------------+------+------+---------+------
        Date    |  Min |  Max | Average | Total
    ------------+------+------+---------+------
     2017-12-19 | 40.5 | 67.0 |    45.5 |  1440
     2017-12-18 | 43.5 | 70.5 |    48.1 |  1439
     2017-12-17 | 43.5 | 65.5 |    55.1 |  1439
     2017-12-16 | 60.5 | 77.5 |    64.1 |  1440
     2017-12-15 | 61.5 | 77.5 |    63.9 |  1440
     2017-12-14 | 57.5 | 77.5 |    62.4 |  1440
     2017-12-13 | 46.0 | 77.0 |    54.1 |  1440
    ------------+------+------+---------+------

    -------------+------
     Temperature | Total
    -------------+------
            67.0 |     1
            66.5 |     3
            66.0 |     1
            65.0 |     4
            64.5 |    18
    -------------+------
        SubTotal |    27
    -------------+------
            64.0 |     4
            63.5 |    11
            63.0 |     2
            62.5 |    10
            62.0 |    10
    -------------+------
        SubTotal |    37
    -------------+------
            61.5 |     5
            61.0 |     1
            60.5 |     5
            60.0 |     2
            59.5 |     1
    -------------+------
        SubTotal |    14
    -------------+------
            59.0 |     1
            58.5 |     4
            57.5 |     3
            57.0 |     1
            56.5 |     6
    -------------+------
        SubTotal |    15
    -------------+------
            56.0 |     1
            55.5 |     2
            53.5 |     4
            53.0 |     5
            52.5 |     5
    -------------+------
        SubTotal |    17
    -------------+------
            52.0 |     3
            51.5 |     2
            51.0 |     4
            50.5 |     8
            50.0 |     7
    -------------+------
        SubTotal |    24
    -------------+------
            49.5 |    13
            49.0 |    14
            48.5 |    19
            48.0 |    22
            47.5 |    24
    -------------+------
        SubTotal |    92
    -------------+------
            47.0 |    27
            46.5 |    56
            46.0 |    79
            45.5 |   105
            45.0 |   188
    -------------+------
        SubTotal |   455
    -------------+------
            44.5 |   162
            44.0 |    73
            43.5 |    40
            43.0 |    53
            42.5 |    70
    -------------+------
        SubTotal |   398
    -------------+------
            42.0 |    96
            41.5 |   161
            41.0 |    96
            40.5 |     8
    -------------+------
        SubTotal |   361
    -------------+------
           Total |  1440
    -------------+------

`

If people are interested in my systemd service: I can share that also.

Comments and tips are appreciated.

I was asked what getDB does. Here it is:
proc getDB {} {
    if {$::argc != 1} {
        error [format "ERROR: %s DATABASE" [getScriptName]]
    }
    sqlite db [lindex $::argv 0]
    db     timeout [expr {10 * 1000}]
}

And it uses getScriptName:
proc getScriptName {} {
    file tail $::argv0
}

I will also share the systemd service and the table and publish the code on GitHub.


Category CPU usage[Category System Maintenance]