SD-card and USB stick statistics script

Moderator: leecollings

Post Reply
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

SD-card and USB stick statistics script

Post by EddyG »

If you ever want to know how much is written to the SD-card and/or USB stick you can use this script.
If you leave it running for some time you can find out at what time data is write to the SD-card and/or USB stick.
This way you can optimize the amount of data written to hardware and expand the lifetime of the SD-card and/or USB stick.
The explanation is in the script.

Code: Select all

--[[
        Scriptname: media-statistics.lua

        Prerequisits
        ==================================
        Domoticz v4.9700 or later (dzVents version 2.4 or later)

-- Author  -----------------------------------------------------------------
        V1.0 - Eddy Geurts

-- Devices  ----------------------------------------------------------------
        Create virtual hardware: Dummy (Does nothing, use for virtual switches only)
        Create set of 2 virtual sensors (use button on the just created virtual device)
        Types:          Custom Sensor
                        Custom Sensor

        One set of 2 Devices for SD-card (device: /dev/mmcblk0)
        If available one set of 2 Devices for USB stick (device: /dev/sda)
        If available one set of 2 Devices for USB stick (device: /dev/sdb)

        Device name                     Axis label

        SD-card number of writes        Writes
        SD-card writes Kb               Kb
        SDA number of writes            Writes
        SDA writes Kb                   Kb
        SDB number of writes            Writes
        SDB writes Kb                   Kb

        If needed, delete below the code of the device(s) that are NOT used.

-- /sys/block/<dev>/stat --- output ----------------------------------------
        Field   Name            units         description
        -----   ----            -----         -----------
        1       read I/Os       requests      number of read I/Os processed
        2       read merges     requests      number of read I/Os merged with in-queue I/O
        3       read sectors    sectors       number of sectors read
        4       read ticks      milliseconds  total wait time for read requests
        5       write I/Os      requests      number of write I/Os processed
        6       write merges    requests      number of write I/Os merged with in-queue I/O
        7       write sectors   sectors       number of sectors written
        8       write ticks     milliseconds  total wait time for write requests
        9       in_flight       requests      number of I/Os currently in flight
        10      io_ticks        milliseconds  total time this block device has been active
        11      time_in_queue   milliseconds  total wait time for all requests

        'sectorsize' can be found by 'sudo fdisk -l' and is mostly 512 (bytes)

 ]]

return {
        active = true,
--      active = false,

        on = {
                ['timer'] = { 'every 5 minutes', }
--              ['timer'] = { 'every minute', }
        },

        logging = {
                level = domoticz.LOG_INFO,
                marker = "Media"
        },

        data = {
                SDcardCounter = {initial=0},
                SDcardBytes = {initial=0},
                SDACounter = {initial=0},
                SDABytes = {initial=0},
--                SDBCounter = {initial=0},
--                SDBBytes = {initial=0}
        },

        execute = function(domoticz)

                function mysplit(inputstr, sep)
                        if sep == nil then
                                sep = "%s"
                        end
                        local t={} ; i=1
                        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                                t[i] = str
                        i = i + 1
                        end
                        return t
                end
                local f, line, results, result
                local sectorsize = 512          -- change if necessary
-- Device mmcblk0
                if domoticz.devices("SD-card number of writes").isDevice and domoticz.devices("SD-card writes Kb").isDevice then
                        f = io.popen("cat /sys/block/mmcblk0/stat")
                        line = f:read("*a")
                        f:close()
                        results = mysplit(line, " ")
                        result = results[5] - domoticz.data.SDcardCounter
                        if result < 0 then result = 0 end
                        domoticz.devices("SD-card number of writes").updateCustomSensor(result)
                        result = domoticz.utils.round(((results[7] - domoticz.data.SDcardBytes) * sectorsize) / 1024)
                        if result < 0 then result = 0 end
                        domoticz.devices("SD-card writes Kb").updateCustomSensor(result)
                        domoticz.log("SD-card: " .. (results[5] - domoticz.data.SDcardCounter) .. " writes with " .. result .. " Kb")
                        domoticz.data.SDcardCounter = results[5]
                        domoticz.data.SDcardBytes = results[7]
                end
-- Device SDA
                if domoticz.devices("SDA number of writes").isDevice and domoticz.devices("SDA writes Kb").isDevice then
                        f = io.popen("cat /sys/block/sda/stat")
                        line = f:read("*a")
                        f:close()
                        results = mysplit(line, " ")
                        result = results[5] - domoticz.data.SDACounter
                        if result < 0 then result = 0 end
                        domoticz.devices("SDA number of writes").updateCustomSensor(result)
                        result = domoticz.utils.round(((results[7] - domoticz.data.SDABytes) * sectorsize) / 1024)
                        if result < 0 then result = 0 end
                        domoticz.devices("SDA writes Kb").updateCustomSensor(result)
                        domoticz.log("SDA: " .. (results[5] - domoticz.data.SDACounter) .. " writes with " .. result .. " Kb")
                        domoticz.data.SDACounter = results[5]
                        domoticz.data.SDABytes = results[7]
                end
-- Device SDB
--              if domoticz.devices("SDB number of writes").isDevice and domoticz.devices("SDB writes Kb").isDevice then
--                      f = io.popen("cat /sys/block/sdb/stat")
--                      line = f:read("*a")
--                      f:close()
--                      results = mysplit(line, " ")
--                      result = results[5] - domoticz.data.SDBCounter
--                      if result < 0 then result = 0 end
--                      domoticz.devices("SDB number of writes").updateCustomSensor(result)
--                      result = domoticz.utils.round(((results[7] - domoticz.data.SDBBytes) * sectorsize) / 1024)
--                      if result < 0 then result = 0 end
--                      domoticz.devices("SDB writes Kb").updateCustomSensor(result)
--                      domoticz.log("SDB: " .. (results[5] - domoticz.data.SDBCounter) .. " writes with " .. result .. " Kb")
--                      domoticz.data.SDBCounter = results[5]
--                      domoticz.data.SDBBytes = results[7]
--              end

        end
}
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests