bin/createRadmonRrd.py
b528647b
 #!/usr/bin/python -u
 ## The -u option above turns off block buffering of python output. This assures
 ## that each error message gets individually printed to the log file.
 #
 # Module: createRadmonRrd.py
 #
 # Description: Creates a rrdtool database for use by the weather agent to
 # store the data from the weather station.  The agent uses the data in the
 # database to generate graphic charts for display in the weather station
 # web page.
 #
 # Copyright 2014 Jeff Owrey
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation, either version 3 of the License, or
 #    (at your option) any later version.
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 #    GNU General Public License for more details.
 #
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see http://www.gnu.org/license.
 #
 # Revision History
eff47ebd
 #   * v11 released 24 Jul 2018 by J L Owrey
b528647b
 #
eff47ebd
 # Example of rrdtool command line executed by this program:
 #
 #   rrdtool create radmonData.rrd --step 30 DS:CPM:GAUGE:60:U:U
 #   DS:SvperHr:GAUGE:60:U:U RRA:LAST:0.5:1:2880 RRA:LAST:0.5:30:35520
 #
 
b528647b
 import os
 import subprocess
 
     ### DEFINE FILE LOCATIONS ###
 
db8a5c09
 _USER = os.environ['USER']
eff47ebd
 _RRD_FILE = "/home/%s/database/radmonData.rrd" % _USER  # rrd database file
b528647b
 _RRD_SIZE_IN_DAYS = 370 # days
db8a5c09
 _1YR_RRA_STEPS_PER_DAY = 96
b528647b
 _DATABASE_UPDATE_INTERVAL = 30
 
 def createRrdFile():
     """Create the rrd file if it does not exist.
        Parameters: none
        Returns: True, if successful
     """
 
     if os.path.exists(_RRD_FILE):
         print "rrdtool radiation database file already exists"
         return True
 
db8a5c09
      ## Calculate database size
  
     heartBeat = 2 * _DATABASE_UPDATE_INTERVAL
eff47ebd
     rra1yrNumPDP =  int(round(86400 / (_1YR_RRA_STEPS_PER_DAY * \
                         _DATABASE_UPDATE_INTERVAL)))
db8a5c09
     rrd24hrNumRows = int(round(86400 / _DATABASE_UPDATE_INTERVAL))
     rrd1yearNumRows = _1YR_RRA_STEPS_PER_DAY * _RRD_SIZE_IN_DAYS
b528647b
        
db8a5c09
     strFmt = ("rrdtool create %s --step %s "
b528647b
                "DS:CPM:GAUGE:%s:U:U DS:SvperHr:GAUGE:%s:U:U "
bd3dc880
                "RRA:LAST:0.5:1:%s RRA:LAST:0.5:%s:%s")
b528647b
 
db8a5c09
     strCmd = strFmt % (_RRD_FILE, _DATABASE_UPDATE_INTERVAL, \
                 heartBeat, heartBeat, rrd24hrNumRows, rra1yrNumPDP, rrd1yearNumRows)
b528647b
 
     print "creating rrdtool radiation database...\n\n%s\n" % strCmd
 
     # Spawn a sub-shell and run the command
     try:
         subprocess.check_output(strCmd, stderr=subprocess.STDOUT, \
                                 shell=True)
     except subprocess.CalledProcessError, exError:
eff47ebd
         print "rrdtool create failed: %s" % (exError.output)
b528647b
         return False
     return True
 ##end def
 
 def main():
     createRrdFile()
 ## end def
 
 if __name__ == '__main__':
     main()