Exemplo n.º 1
0
def do_markdown(flock, fdata):
  home              = os.path.expanduser('~')
  uname             = os.uname()

  fi = home + "/.kamstrupd.branch"
  with open(fi, 'r') as f:
    kamstrupbranch  = f.read().strip('\n')

  mf.lock(flock)
  shutil.copyfile(home + '/' + MYAPP + '/default.md', fdata)

  with open(fdata, 'a') as f:
    mf.syslog_trace("writing {0}".format(fdata), False, DEBUG)

    f.write('![A GNUplot image should be here: kamstrup11.png](img/kamstrup11.png)\n')
    f.write('![A GNUplot image should be here: kamstrup12.png](img/kamstrup12.png)\n')
    f.write('![A GNUplot image should be here: kamstrup13.png](img/kamstrup13.png)\n')

    # System ID
    f.write('!!! ')
    f.write(uname[0] + ' ' + uname[1] + ' ' + uname[2] + ' ' + uname[3] + ' ' + uname[4] + ' ' + platform.platform() + '  \n')

    # branch
    f.write('!!! kamstrupd   on: ' + kamstrupbranch + '\n\n')

  mf.unlock(flock)
Exemplo n.º 2
0
def do_report(result, flock, fdata):
  # Get the time and date in human-readable form and UN*X-epoch...
  outDate  = time.strftime('%Y-%m-%dT%H:%M:%S')
  outEpoch = int(time.strftime('%s'))
  # round to current minute to ease database JOINs
  # outEpoch = outEpoch - (outEpoch % 60)
  result   = ', '.join(map(str, result))
  mf.lock(flock)
  mf.syslog_trace("   @: {0}s".format(outDate), False, DEBUG)
  with open(fdata, 'a') as f:
    f.write('{0}, {1}, {2}\n'.format(outDate, outEpoch, result))
  mf.unlock(flock)
Exemplo n.º 3
0
def do_report(result, flock, fdata):
    time.sleep(1)  # sometimes the function is called a sec too soon.
    # Get the time and date in human-readable form and UN*X-epoch...
    outDate = time.strftime('%Y-%m-%dT%H:%M:%S')
    outEpoch = int(time.strftime('%s'))
    # round to current minute to ease database JOINs
    outEpoch = outEpoch - (outEpoch % 60)
    result = ', '.join(map(str, result))
    ident = NODE + '@' + str(outEpoch)
    mf.syslog_trace(">>> ID : {0}  -  {1}".format(ident, outDate), False,
                    DEBUG)
    mf.lock(flock)
    with open(fdata, 'a') as f:
        f.write('{0}, {1}, {2}, {3}, {4}\n'.format(outDate, outEpoch, NODE,
                                                   result, ident))
    mf.unlock(flock)
Exemplo n.º 4
0
def gettelegram():
  # flag used to exit the while-loop
  abort = 0
  # countdown counter used to prevent infinite loops
  loops2go = 40
  # storage space for the telegram
  telegram = []
  # end of line delimiter
  # delim = "\x0a"

  while abort == 0:
    try:
      # line = "".join(iter(lambda: port.read(1), delim)).strip()
      line = str(port.readline().strip(), 'utf-8')
      if line == "!":
        abort = 1
      if line != "":
        telegram.append(line)
    except Exception:
      mf.syslog_trace("*** Serialport read error:", syslog.LOG_CRIT, DEBUG)
      mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
      abort = 2

    loops2go = loops2go - 1
    if loops2go < 0:
      abort = 3

  # test for correct start of telegram
  if telegram[0][0] != "/":
    abort = 2

  # Return codes:
  # abort == 1 indicates a successful read
  # abort == 2 means that a serial port read/write error occurred
  # abort == 3 no valid data after several attempts
  return telegram, abort
Exemplo n.º 5
0
def do_work():
    # 5 datapoints gathered here
    #
    sda.smart()
    sdb.smart()
    sdc.smart()
    sdd.smart()
    sde.smart()
    # sdf.smart()
    # sdg.smart()

    # disktemperature
    Tsda = sda.getdata('194')
    Tsdb = sdb.getdata('194')
    Tsdc = sdc.getdata('194')
    Tsdd = sdd.getdata('194')
    Tsde = sde.getdata('194')
    # Tsdf = 0
    # Tsdg = 0

    mf.syslog_trace(
        '{0}, {1}, {2}, {3}, {4}'.format(Tsda, Tsdb, Tsdc, Tsdd, Tsde), False,
        DEBUG)
    return '{0}, {1}, {2}, {3}, {4}'.format(Tsda, Tsdb, Tsdc, Tsdd, Tsde)
Exemplo n.º 6
0
def upload_page(script):
    try:
        # Upload the webpage and graphs
        if os.path.isfile('/tmp/' + MYAPP + '/site/text.md'):
            write_lftp(script)
            cmnd = ['lftp', '-f', script]
            mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
            cmnd = subprocess.check_output(cmnd, timeout=20)
            mf.syslog_trace("...uploadpag:  {0}".format(cmnd), False, DEBUG)
    except subprocess.TimeoutExpired:
        mf.syslog_trace("***TIMEOUT***:  {0}".format(cmnd), syslog.LOG_ERR,
                        DEBUG)
        time.sleep(17 * 60)  # wait 17 minutes for the router to restart.
        pass
    except subprocess.CalledProcessError:
        mf.syslog_trace("***ERROR***:    {0}".format(cmnd), syslog.LOG_ERR,
                        DEBUG)
        time.sleep(17 * 60)  # wait 17 minutes for the router to restart.
        pass
Exemplo n.º 7
0
def do_markdown(flock, fdata, hwdevice):
    home = os.path.expanduser('~')
    uname = os.uname()
    Tcpu = "(no T-sensor)"
    fcpu = "(no f-sensor)"
    mds = ""
    if os.path.isfile(hwdevice):
        fi = hwdevice
        with open(fi, 'r') as f:
            Tcpu = float(f.read().strip('\n')) / 1000

    if os.path.isfile('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq'):
        fi = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
        with open(fi, 'r') as f:
            fcpu = float(f.read().strip('\n')) / 1000
    fcpu_stats = str(subprocess.check_output(["cpufreq-info", "-sm"]), 'utf-8')

    fi = home + "/.lnxdiagd.branch"
    with open(fi, 'r') as f:
        lnxdiagdbranch = f.read().strip('\n')

    if os.path.isfile("/proc/mdstat"):
        mds = "-\n" + str(subprocess.check_output(["cat", "/proc/mdstat"]),
                          'utf-8')
    uptime = str(subprocess.check_output(["uptime"]), 'utf-8')
    # dfh               = str(subprocess.check_output(["df", "-h"]), 'utf-8')
    # freeh             = str(subprocess.check_output(["free", "-h"]), 'utf-8')
    # p1                = subprocess.Popen(["ps", "-e", "-o", "pcpu,args"],           stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # p2                = subprocess.Popen(["cut", "-c", "-132"],   stdin=p1.stdout,  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # p3                = subprocess.Popen(["awk", "NR>2"],         stdin=p2.stdout,  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # p4                = subprocess.Popen(["sort", "-nr"],         stdin=p3.stdout,  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # p5                = subprocess.Popen(["head", "-10"],         stdin=p4.stdout,  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # ps, err            = p5.communicate()
    # psout             = str(ps, 'utf-8')

    mf.lock(flock)

    with open(fdata, 'w') as f:
        mf.syslog_trace("writing {0}".format(fdata), False, DEBUG)
        # YAML header
        f.write('---\n')
        f.write('title: ' + NODE + '\n')
        f.write('menu: ' + NODE + '\n')
        f.write('---\n')

        # HEADER
        f.write('# ' + NODE + '\n\n')

        # System ID
        f.write('!!! ')
        f.write(uname[0] + ' ' + uname[2] + ' ' + uname[3] + ' ' + uname[4] +
                ' ' + platform.platform() + '  \n')

        # lnxdiagd branch
        f.write('!!! lnxdiagd   on: ' + lnxdiagdbranch + '\n\n')

        # System Uptime
        f.write('### Server Uptime:  \n')
        f.write('!!! ')
        f.write(uptime)
        f.write('\n')

        # CPU temperature and frequency
        f.write('### Server Temperature:  \n')
        f.write('!! ' + str(Tcpu) + ' degC @ ' + str(fcpu) +
                ' MHz    stats: ' + fcpu_stats + '\n\n')
        f.write('### Server Graphs:  \n')
        if (hwdevice != "nohwdevice"):
            f.write(
                '![A GNUplot image should be here: day11.png](img/day11.png)\n'
            )
            # f.write('![A GNUplot image should be here: day11.png](img/day11.old.png)\n')
        f.write(
            '![A GNUplot image should be here: day12.png](img/day12.png)\n')
        f.write(
            '![A GNUplot image should be here: day14.png](img/day14.png)\n')
        f.write(
            '![A GNUplot image should be here: day13.png](img/day13.png)\n')
        f.write(
            '![A GNUplot image should be here: day15.png](img/day15.png)\n')
        if (NODE == "boson"):
            f.write(
                '![A GNUplot image should be here: day19.png](img/day19.png)\n'
            )

        # # Disk usage
        # f.write('## Disk Usage\n')
        # f.write(dfh)      # dfh comes with its own built-in '/n'
        if (NODE == "boson"):
            f.write('```\n')
            f.write(mds)  # mds comes with its own built-in '/n'
            f.write('```\n\n')

        if (NODE == "boson"):
            rbc_sda = sda.getdata('5')
            rbc_sdb = sdb.getdata('5')
            rbc_sdc = sdc.getdata('5')
            rbc_sdd = sdd.getdata('5')
            rbc_sde = sde.getdata('5')
            # ou_sda = sda.getdata('198')
            ou_sdb = sdb.getdata('198')
            ou_sdc = sdc.getdata('198')
            ou_sdd = sdd.getdata('198')
            ou_sde = sde.getdata('198')
            # disktemperature
            temperature_sda = sda.getdata('194')
            temperature_sdb = sdb.getdata('194')
            temperature_sdc = sdc.getdata('194')
            temperature_sdd = sdd.getdata('194')
            temperature_sde = sde.getdata('194')
            # disk power-on time
            pwron_time_a = sda.getdata('9')
            pwron_time_b = sdb.getdata('9')
            pwron_time_c = sdc.getdata('9')
            pwron_time_d = sdd.getdata('9')
            pwron_time_e = sde.getdata('9')
            # disk health
            health_sda = sda.gethealth()
            health_sdb = sdb.gethealth()
            health_sdc = sdc.gethealth()
            health_sdd = sdd.gethealth()
            health_sde = sde.gethealth()
            # Self-test info
            test_sda = sda.getlasttest()  # noqa
            test_sdb = sdb.getlasttest()
            test_sdc = sdc.getlasttest()
            test_sdd = sdd.getlasttest()
            test_sde = sde.getlasttest()
            # Disk info
            info_sda = sda.getinfo()
            info_sdb = sdb.getinfo()
            info_sdc = sdc.getinfo()
            info_sdd = sdd.getinfo()
            info_sde = sde.getinfo()
            f.write('```\n')
            f.write('SSD: ' + temperature_sda + ' || disk1: ' +
                    temperature_sdb + ' || disk2: ' + temperature_sdc +
                    ' || disk3: ' + temperature_sdd + ' || disk4: ' +
                    temperature_sde + ' [degC]\n')
            f.write('\n')
            f.write('---SSD---\n')
            f.write(' Name      : ' + info_sda + '\n')
            f.write(' PowerOn   : ' + pwron_time_a + '\n')
            # f.write(' Last test : ' + test_sda + '\n')
            if "PASSED" not in health_sda:
                f.write('             ' + health_sda + '\n')
            if not (rbc_sda == "0"):
                f.write('              Retired Block Count (5) = ' + rbc_sda +
                        '\n')

            f.write('---disk1---\n')
            f.write(' Name      : ' + info_sdb + '\n')
            f.write(' PowerOn   : ' + pwron_time_b + '\n')
            if "without" not in test_sdb:
                f.write(' Last test : ' + test_sdb + '\n')
            if "PASSED" not in health_sdb:
                f.write('             ' + health_sdb + '\n')
            if not (rbc_sdb == "0") or not (ou_sdb == "0"):
                f.write('              Retired Block Count (5) = ' + rbc_sdb +
                        ' - Offline Uncorrectable (198) = ' + ou_sdb + '\n')

            f.write('---disk2---\n')
            f.write(' Name      : ' + info_sdc + '\n')
            f.write(' PowerOn   : ' + pwron_time_c + '\n')
            if "without" not in test_sdc:
                f.write(' Last test : ' + test_sdc + '\n')
            if "PASSED" not in health_sdc:
                f.write('             ' + health_sdc + '\n')
            if not (rbc_sdc == "0") or not (ou_sdc == "0"):
                f.write('              Retired Block Count (5) = ' + rbc_sdc +
                        ' - Offline Uncorrectable (198) = ' + ou_sdc + '\n')

            f.write('---disk3---\n')
            f.write(' Name      : ' + info_sdd + '\n')
            f.write(' PowerOn   : ' + pwron_time_d + '\n')
            if "without" not in test_sdd:
                f.write(' Last test : ' + test_sdd + '\n')
            if "PASSED" not in health_sdd:
                f.write('             ' + health_sdd + '\n')
            if not (rbc_sdd == "0") or not (ou_sdd == "0"):
                f.write('              Retired Block Count (5) = ' + rbc_sdd +
                        ' - Offline Uncorrectable (198) = ' + ou_sdd + '\n')

            f.write('---disk4---\n')
            f.write(' Name      : ' + info_sde + '\n')
            f.write(' PowerOn   : ' + pwron_time_e + '\n')
            if "without" not in test_sde:
                f.write(' Last test : ' + test_sde + '\n')
            if "PASSED" not in health_sde:
                f.write('             ' + health_sde + '\n')
            if not (rbc_sde == "0") or not (ou_sde == "0"):
                f.write('              Retired Block Count (5) = ' + rbc_sde +
                        ' - Offline Uncorrectable (198) = ' + ou_sde + '\n')
            f.write(' ')
            f.write('```\n\n')

        # # Memory usage
        # f.write('## Memory Usage\n')
        # f.write('```\n')
        # f.write(freeh)    # freeh comes with its own built-in '/n'
        # f.write('```\n\n')

        # # Top 10 processes
        # f.write('## Top 10 processes:\n')
        # f.write('```\n')
        # f.write(psout)    # psout comes with its own built-in '/n'
        # f.write('```\n\n')

    mf.unlock(flock)
Exemplo n.º 8
0
    def run():
        iniconf = configparser.ConfigParser()
        inisection = MYID
        home = os.path.expanduser('~')
        s = iniconf.read(home + '/' + MYAPP + '/config.ini')
        mf.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
        mf.syslog_trace(
            "Options       : {0}".format(iniconf.items(inisection)), False,
            DEBUG)
        reporttime = iniconf.getint(inisection, "reporttime")
        # cycles          = iniconf.getint(inisection, "cycles")
        samplespercycle = iniconf.getint(inisection, "samplespercycle")
        flock = iniconf.get(inisection, "lockfile")
        fdata = iniconf.get(inisection, "markdown")

        # samples         = samplespercycle * cycles          # total number of samples averaged
        sampletime = reporttime / samplespercycle  # time [s] between samples
        # cycleTime       = samples * sampletime              # time [s] per cycle

        try:
            hwdevice = iniconf.get("11", NODE + ".hwdevice")
        except configparser.NoOptionError:  # no hwdevice
            hwdevice = "nohwdevice"
            pass

        while True:
            try:
                starttime = time.time()

                do_markdown(flock, fdata, hwdevice)

                waittime = sampletime - (time.time() -
                                         starttime) - (starttime % sampletime)
                if (waittime > 0):
                    mf.syslog_trace("Waiting  : {0}s".format(waittime), False,
                                    DEBUG)
                    mf.syslog_trace("................................", False,
                                    DEBUG)
                    time.sleep(waittime)
            except Exception:
                mf.syslog_trace("Unexpected error in run()", syslog.LOG_CRIT,
                                DEBUG)
                mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
                raise
Exemplo n.º 9
0
def getsqldata(homedir, nu):
  minit = int(time.strftime('%M'))
  nowur = int(time.strftime('%H'))
  # data of last hour is updated every 3 minutes
  if ((minit % 3) == 0):
    cmnd = homedir + '/' + MYAPP + '/getsqlhour.sh'
    mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
    cmnd = subprocess.call(cmnd)
    mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
  # data of the last day is updated every 30 minutes
  if nu or ((minit % 30) == (SQLMNT % 30)):
    cmnd = homedir + '/' + MYAPP + '/getsqlday.sh'
    mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
    cmnd = subprocess.call(cmnd)
    mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
  # dat of the last week is updated every 4 hours
  if nu or ((nowur % 4) == (SQLHR % 4) and (minit == SQLHRM)):
    cmnd = homedir + '/' + MYAPP + '/getsqlweek.sh'
    mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
    cmnd = subprocess.call(cmnd)
    mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
Exemplo n.º 10
0
def do_mv_data(flock, homedir, script):
  # wait 4 seconds for processes to finish
  # unlock(flock)  # remove stale lock
  time.sleep(4)
  minit = int(time.strftime('%M'))

  # Retrieve data from MySQL database
  getsqldata(homedir, False)

  # Create the graphs based on the MySQL data every 3rd minute
  if ((minit % 3) == 0):
    cmnd = homedir + '/' + MYAPP + '/graphs.sh'
    mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
    cmnd = subprocess.call(cmnd)
    mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)

  try:
    # Upload the webpage and graphs
    if os.path.isfile('/tmp/' + MYAPP + '/site/default.md'):
      write_lftp(script)
      cmnd = ['lftp', '-f', script]
      mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
      cmnd = subprocess.check_output(cmnd, timeout=20)
      mf.syslog_trace("...:  {0}".format(cmnd), False, DEBUG)
  except subprocess.TimeoutExpired:
    mf.syslog_trace("***TIMEOUT***:  {0}".format(cmnd), syslog.LOG_ERR, DEBUG)
    time.sleep(17*60)             # wait 17 minutes for the router to restart.
    pass
  except subprocess.CalledProcessError:
    mf.syslog_trace("***ERROR***:    {0}".format(cmnd), syslog.LOG_ERR, DEBUG)
    time.sleep(17*60)             # wait 17 minutes for the router to restart.
    pass
Exemplo n.º 11
0
  def run(self):
    iniconf         = configparser.ConfigParser()
    inisection      = MYID
    home            = os.path.expanduser('~')
    s               = iniconf.read(home + '/' + MYAPP + '/config.ini')
    mf.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
    mf.syslog_trace("Options       : {0}".format(iniconf.items(inisection)), False, DEBUG)
    mf.syslog_trace("getsqlday.sh  runs every 30 minutes starting at minute {0}".format(SQLMNT), syslog.LOG_DEBUG, DEBUG)
    mf.syslog_trace("getsqlweek.sh runs every 4th hour  starting  at hour   {0}:{1}".format(SQLHR, SQLHRM), syslog.LOG_DEBUG, DEBUG)
    reportTime      = iniconf.getint(inisection, "reporttime")
    samplesperCycle = iniconf.getint(inisection, "samplespercycle")
    flock           = iniconf.get(inisection, "lockfile")

    scriptname      = iniconf.get(inisection, "lftpscript")

    sampleTime      = reportTime/samplesperCycle         # time [s] between samples
    getsqldata(home, True)
    while True:
      try:
        startTime   = time.time()

        do_mv_data(flock, home, scriptname)

        waitTime    = sampleTime - (time.time() - startTime) - (startTime % sampleTime)
        if (waitTime > 0):
          mf.syslog_trace("Waiting  : {0}s".format(waitTime), False, DEBUG)
          mf.syslog_trace("................................", False, DEBUG)
          time.sleep(waitTime)
      except Exception:
        mf.syslog_trace("Unexpected error in run()", syslog.LOG_CRIT, DEBUG)
        mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
        raise
Exemplo n.º 12
0
  def run(self):
    iniconf         = configparser.ConfigParser()
    inisection      = MYID
    home            = os.path.expanduser('~')
    s               = iniconf.read(home + '/' + MYAPP + '/config.ini')
    mf.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
    mf.syslog_trace("Options       : {0}".format(iniconf.items(inisection)), False, DEBUG)
    reportTime      = iniconf.getint(inisection, "reporttime")
    # cycles          = iniconf.getint(inisection, "cycles")
    samplesperCycle = iniconf.getint(inisection, "samplespercycle")
    flock           = iniconf.get(inisection, "lockfile")
    fdata           = iniconf.get(inisection, "markdown")

    # samples         = samplesperCycle * cycles          # total number of samples averaged
    sampleTime      = reportTime/samplesperCycle        # time [s] between samples
    # cycleTime       = samples * sampleTime              # time [s] per cycle

    while True:
      try:
        startTime   = time.time()

        do_markdown(flock, fdata)

        waitTime    = sampleTime - (time.time() - startTime) - (startTime % sampleTime)
        if (waitTime > 0):
          mf.syslog_trace("Waiting  : {0}s".format(waitTime), False, DEBUG)
          mf.syslog_trace("................................", False, DEBUG)
          time.sleep(waitTime)
      except Exception:
        mf.syslog_trace("Unexpected error in run()", syslog.LOG_CRIT, DEBUG)
        mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
        raise
Exemplo n.º 13
0
def do_writesample(cnsql, cmd, sample):
  fail2write  = False
  dat         = (sample.split(', '))
  try:
    cursql    = cnsql.cursor()
    mf.syslog_trace("   Data: {0}".format(dat), False, DEBUG)
    cursql.execute(cmd, dat)
    cnsql.commit()
    cursql.close()
  except mdb.IntegrityError:
    mf.syslog_trace(" ***** MySQL ERROR *****", syslog.LOG_ERR, DEBUG)
    mf.syslog_trace(" *** DB error : {0}".format(sys.exc_info()[1]), syslog.LOG_ERR,  DEBUG)
    if cursql:
      cursql.close()
      mf.syslog_trace(" *I* Closed MySQL connection in do_writesample()", syslog.LOG_ERR, DEBUG)
      mf.syslog_trace(" *** Execution of MySQL command {0} FAILED!".format(cmd), syslog.LOG_ERR, DEBUG)
      mf.syslog_trace(" *** Not added to MySQLdb: {0}".format(dat), syslog.LOG_ERR, DEBUG)
      mf.syslog_trace(" ***** MySQL ERROR *****", syslog.LOG_ERR, DEBUG)
    pass
  except mdb.OperationalError as e:
    mf.syslog_trace(" ***** MySQL ERROR *****", syslog.LOG_ERR, DEBUG)
    mf.syslog_trace(" *** DB error : {0}".format(sys.exc_info()[1]), syslog.LOG_ERR,  DEBUG)
    fail2write = True
    if cursql:
      cursql.close()
      mf.syslog_trace(" *O* Closed MySQL connection in do_writesample()", syslog.LOG_ERR, DEBUG)
      mf.syslog_trace(" *** Execution of MySQL command {0} FAILED!".format(cmd), syslog.LOG_ERR, DEBUG)
      mf.syslog_trace(" *** Not added to MySQLdb: {0}".format(dat), syslog.LOG_ERR, DEBUG)
      mf.syslog_trace(" ***** MySQL ERROR *****", syslog.LOG_ERR, DEBUG)
    if e.args[0] in (mdbcr.SERVER_GONE_ERROR, mdbcr.SERVER_LOST):
      time.sleep(17*60)             # wait 17 minutes for the server to return.
      raise
    else:
      pass

  return fail2write
Exemplo n.º 14
0
  def run():
    try:                 # Initialise MySQLdb
      consql    = mdb.connect(host='sql', db='domotica', read_default_file='~/.my.cnf')
      if consql.open:    # dB initialised succesfully -> get a cursor on the dB.
        cursql  = consql.cursor()
        cursql.execute("SELECT VERSION()")
        versql  = cursql.fetchone()
        cursql.close()
        logtext = "{0} : {1}".format("Attached to MySQL server", versql)
        syslog.syslog(syslog.LOG_INFO, logtext)
    except mdb.Error:
      mf.syslog_trace("Unexpected MySQL error in run(init)", syslog.LOG_CRIT, DEBUG)
      mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
      if consql.open:    # attempt to close connection to MySQLdb
        consql.close()
        mf.syslog_trace(" ** Closed MySQL connection in run() **", syslog.LOG_CRIT, DEBUG)
      raise

    iniconf         = configparser.ConfigParser()
    inisection      = MYID
    home            = os.path.expanduser('~')
    s               = iniconf.read(home + '/' + MYAPP + '/config.ini')
    mf.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
    mf.syslog_trace("Options       : {0}".format(iniconf.items(inisection)), False, DEBUG)
    reporttime      = iniconf.getint(inisection, "reporttime")
    # cycles          = iniconf.getint(inisection, "cycles")
    samplespercycle = iniconf.getint(inisection, "samplespercycle")
    flock           = iniconf.get(inisection, "lockfile")

    # samples         = samplespercycle * cycles              # total number of samples averaged
    sampletime      = reporttime/samplespercycle         # time [s] between samples
    # cycleTime       = samples * sampletime                # time [s] per cycle

    while True:
      try:
        starttime   = time.time()

        do_sql_data(flock, iniconf, consql)

        waittime    = sampletime - (time.time() - starttime)  # - (starttime % sampletime)
        if (waittime > 0):
          mf.syslog_trace("Waiting  : {0}s".format(waittime), False, DEBUG)
          mf.syslog_trace("................................", False, DEBUG)
          time.sleep(waittime)
      except Exception:
        mf.syslog_trace("Unexpected error in run()", syslog.LOG_CRIT, DEBUG)
        mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
        # attempt to close connection to MySQLdb
        if consql.open:
          consql.close()
          mf.syslog_trace(" *** Closed MySQL connection in run() ***", syslog.LOG_CRIT, DEBUG)
        raise
Exemplo n.º 15
0
def do_sql_data(flock, inicnfg, cnsql):
  mf.syslog_trace("============================", False, DEBUG)
  mf.syslog_trace("Pushing data to MySQL-server", False, DEBUG)
  mf.syslog_trace("============================", False, DEBUG)
  mf.unlock(flock)  # remove stale lock
  time.sleep(2)
  mf.lock(flock)
  # wait for all other processes to release their locks.
  count_internal_locks = 2
  while (count_internal_locks > 1):
    time.sleep(1)
    count_internal_locks = 0
    for fname in glob.glob(r'/tmp/' + MYAPP + '/*.lock'):
      count_internal_locks += 1
    mf.syslog_trace("{0} internal locks exist".format(count_internal_locks), False, DEBUG)
  # endwhile

  for inisect in inicnfg.sections():  # Check each section of the config.ini file
    errsql = False
    try:
      ifile = inicnfg.get(inisect, "resultfile")
      mf.syslog_trace(" < {0}".format(ifile), False, DEBUG)

      try:
        sqlcmd = []
        sqlcmd = inicnfg.get(inisect, "sqlcmd")
        mf.syslog_trace("   CMD : {0}".format(sqlcmd), False, DEBUG)

        data = mf.cat(ifile).splitlines()
        if data:
          for entry in range(0, len(data)):
            errsql = do_writesample(cnsql, sqlcmd, data[entry])
          # endfor
        # endif
      except configparser.NoOptionError:  # no sqlcmd
        mf.syslog_trace("*1* {0}".format(sys.exc_info()[1]), False, DEBUG)
    except configparser.NoOptionError:  # no ifile
      mf.syslog_trace("*2* {0}".format(sys.exc_info()[1]), False, DEBUG)

    try:
      if os.path.isfile(ifile) and not errsql:
        # IF resultfile exists AND SQL-job was successful or non-existing
        mf.syslog_trace("Deleting {0}".format(ifile), False, DEBUG)
        os.remove(ifile)
    except configparser.NoOptionError:  # no ofile
      mf.syslog_trace("*3* {0}".format(sys.exc_info()[1]), False, DEBUG)

  # endfor
  mf.unlock(flock)
Exemplo n.º 16
0
  def run(self):
    iniconf         = configparser.ConfigParser()
    inisection      = MYID
    home            = os.path.expanduser('~')
    s               = iniconf.read(home + '/' + MYAPP + '/config.ini')
    mf.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
    mf.syslog_trace("Options       : {0}".format(iniconf.items(inisection)), False, DEBUG)
    reportTime      = iniconf.getint(inisection, "reporttime")
    cycles          = iniconf.getint(inisection, "cycles")
    samplesperCycle = iniconf.getint(inisection, "samplespercycle")
    flock           = iniconf.get(inisection, "lockfile")
    fdata           = iniconf.get(inisection, "resultfile")

    samples         = samplesperCycle * cycles           # total number of samples averaged
    sampleTime      = reportTime/samplesperCycle         # time [s] between samples

    data            = []                                 # array for holding sampledata
    # raw             = [0] * 8                            # array for holding previous

    port.open()
    serial.XON
    while True:
      try:
        startTime     = time.time()

        result        = do_work()
        result        = result.split(',')
        mf.syslog_trace("Result   : {0}".format(result), False, DEBUG)
        # data.append(list(map(int, result)))
        data.append([int(d) for d in result])
        if (len(data) > samples):
          data.pop(0)
        mf.syslog_trace("Data     : {0}".format(data),   False, DEBUG)

        # report sample average
        if (startTime % reportTime < sampleTime):
          # somma       = list(map(sum, zip(*data)))
          somma = [sum(d) for d in zip(*data)]
          # not all entries should be float
          # ['3088596', '3030401', '270', '0', '0', '0', '1', '1']
          # averages    = [format(sm / len(data), '.2f') for sm in somma]
          averages = data[-1]
          averages[2]  = int(somma[2] / len(data))  # avg powerin
          averages[5]  = int(somma[5] / len(data))  # avg powerout
          mf.syslog_trace("Averages : {0}".format(averages),  False, DEBUG)
          if averages[0] > 0:
            do_report(averages, flock, fdata)

        waitTime    = sampleTime - (time.time() - startTime) - (startTime % sampleTime)
        if (waitTime > 0):
          mf.syslog_trace("Waiting  : {0}s".format(waitTime), False, DEBUG)
          mf.syslog_trace("................................", False, DEBUG)
          # no need to wait for the next cycles
          # the meter will pace the meaurements
          # any required waiting will be inside gettelegram()
          # time.sleep(waitTime)
        else:
          mf.syslog_trace("Behind   : {0}s".format(waitTime), False, DEBUG)
          mf.syslog_trace("................................", False, DEBUG)
      except Exception:
        mf.syslog_trace("Unexpected error in run()", syslog.LOG_CRIT, DEBUG)
        mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
        raise
Exemplo n.º 17
0
  def run():
    iniconf         = configparser.ConfigParser()
    inisection      = MYID
    home            = os.path.expanduser('~')
    s               = iniconf.read(home + '/' + MYAPP + '/config.ini')
    mf.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
    mf.syslog_trace("Options       : {0}".format(iniconf.items(inisection)), False, DEBUG)
    reporttime      = iniconf.getint(inisection, "reporttime")
    # cycles          = iniconf.getint(inisection, "cycles")
    samplespercycle = iniconf.getint(inisection, "samplespercycle")
    flock           = iniconf.get(inisection, "lockfile")
    fdata           = iniconf.get(inisection, "resultfile")
    try:
      netdevice     = iniconf.get(inisection, NODE+".net")
    except configparser.NoOptionError:  # no netdevice
      netdevice     = "eth0"
    mf.syslog_trace("Monitoring device: {0}".format(netdevice), syslog.LOG_DEBUG, DEBUG)

    # samples         = samplespercycle * cycles          # total number of samples averaged
    sampletime      = reporttime/samplespercycle        # time [s] between samples
    # cycleTime       = samples * sampletime              # time [s] per cycle

    data            = []                                # array for holding sampledata

    while True:
      try:
        starttime   = time.time()

        result      = do_work(netdevice).split(',')

        data        = list(map(int, result))
        mf.syslog_trace("Data     : {0}".format(data), False, DEBUG)

        # report sample average
        if (starttime % reporttime < sampletime):
          averages  = data
          # averages = sum(data[:]) / len(data)
          # if DEBUG: print averages
          mf.syslog_trace("Averages : {0}".format(averages), False, DEBUG)
          do_report(averages, flock, fdata)

        waittime    = sampletime - (time.time() - starttime) - (starttime % sampletime)
        if (waittime > 0):
          mf.syslog_trace("Waiting  : {0}s".format(waittime), False, DEBUG)
          mf.syslog_trace("................................", False, DEBUG)
          time.sleep(waittime)
      except Exception:
        mf.syslog_trace("Unexpected error in run()", syslog.LOG_CRIT, DEBUG)
        mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
        raise
Exemplo n.º 18
0
    def run():
        iniconf = configparser.ConfigParser()
        inisection = MYID
        s = iniconf.read(HOME + '/' + MYAPP + '/config.ini')
        mf.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
        mf.syslog_trace(
            "Options       : {0}".format(iniconf.items(inisection)), False,
            DEBUG)
        reporttime = iniconf.getint(inisection, "reporttime")
        samplespercycle = iniconf.getint(inisection, "samplespercycle")
        flock = iniconf.get(inisection, "lockfile")
        scriptname = iniconf.get(inisection, "lftpscript")

        sampletime = reporttime / samplespercycle  # time [s] between samples
        sqldata.fetch()
        if (trendgraph.make() == 0):
            upload_page(scriptname)
        while True:
            try:
                # starttime   = time.time()

                do_stuff(flock, HOME, scriptname)
                # not syncing to top of the minute
                waittime = sampletime  # - (time.time() - starttime) - (starttime % sampletime)
                if (waittime > 0):
                    mf.syslog_trace("Waiting  : {0}s".format(waittime), False,
                                    DEBUG)
                    mf.syslog_trace("................................", False,
                                    DEBUG)
                    time.sleep(waittime)
            except Exception:
                mf.syslog_trace("Unexpected error in run()", syslog.LOG_CRIT,
                                DEBUG)
                mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
                raise
Exemplo n.º 19
0
            outDate, outEpoch, NODE, sdd.id, result[3],
            sdd.id + '@' + str(outEpoch)))
        f.write('{0}, {1}, {2}, {3}, {4}, {5}\n'.format(
            outDate, outEpoch, NODE, sde.id, result[4],
            sde.id + '@' + str(outEpoch)))
    mf.unlock(flock)


if __name__ == "__main__":
    daemon = MyDaemon('/tmp/' + MYAPP + '/' + MYID + '.pid')
    if len(sys.argv) == 2:
        if 'start' == sys.argv[1]:
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
        elif 'debug' == sys.argv[1]:
            # assist with debugging.
            print("Debug-mode started. Use <Ctrl>+C to stop.")
            DEBUG = True
            mf.syslog_trace("Daemon logging is ON", syslog.LOG_DEBUG, DEBUG)
            daemon.run()
        else:
            print("Unknown command")
            sys.exit(2)
        sys.exit(0)
    else:
        print("usage: {0!s} start|stop|restart|debug".format(sys.argv[0]))
        sys.exit(2)
Exemplo n.º 20
0
    f.write('set cmd:fail-exit yes;\n')
    f.write('open hendrixnet.nl;\n')
    f.write('cd 05.stream/;\n')
    f.write('set cmd:fail-exit no;\n')
    f.write('mirror --reverse --delete --verbose=3 -c /tmp/' + MYAPP + '/site/ . ;\n')
    f.write('\n')


if __name__ == "__main__":
  daemon = MyDaemon('/tmp/' + MYAPP + '/' + MYID + '.pid')
  if len(sys.argv) == 2:
    if 'start' == sys.argv[1]:
      daemon.start()
    elif 'stop' == sys.argv[1]:
      daemon.stop()
    elif 'restart' == sys.argv[1]:
      daemon.restart()
    elif 'foreground' == sys.argv[1]:
      # assist with debugging.
      print("Debug-mode started. Use <Ctrl>+C to stop.")
      DEBUG = True
      mf.syslog_trace("Daemon logging is ON", syslog.LOG_DEBUG, DEBUG)
      daemon.run()
    else:
      print("Unknown command")
      sys.exit(2)
    sys.exit(0)
  else:
    print("usage: {0!s} start|stop|restart|foreground".format(sys.argv[0]))
    sys.exit(2)
Exemplo n.º 21
0
    def run():
        iniconf = configparser.ConfigParser()
        inisection = MYID
        home = os.path.expanduser('~')
        s = iniconf.read(home + '/' + MYAPP + '/config.ini')
        mf.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
        mf.syslog_trace(
            "Options       : {0}".format(iniconf.items(inisection)), False,
            DEBUG)
        reporttime = iniconf.getint(inisection, "reporttime")
        cycles = iniconf.getint(inisection, "cycles")
        samplespercycle = iniconf.getint(inisection, "samplespercycle")
        flock = iniconf.get(inisection, "lockfile")
        fdata = iniconf.get(inisection, "resultfile")

        samples = samplespercycle * cycles  # total number of samples averaged
        sampletime = reporttime / samplespercycle  # time [s] between samples

        data = []  # array for holding sampledata

        while True:
            try:
                starttime = time.time()

                result = do_work()
                result = result.split(',')
                mf.syslog_trace("Result   : {0}".format(result), False, DEBUG)

                data.append(list(map(float, result)))
                if (len(data) > samples):
                    data.pop(0)
                mf.syslog_trace("Data     : {0}".format(data), False, DEBUG)

                # report sample average
                if (starttime % reporttime < sampletime):
                    somma = list(map(sum, list(zip(*data))))
                    # not all entries should be float
                    # 0.37, 0.18, 0.17, 4, 143, 32147, 3, 4, 93, 0, 0
                    averages = [format(sm / len(data), '.3f') for sm in somma]
                    # Report the last measurement for these parameters:
                    mf.syslog_trace("Averages : {0}".format(averages), False,
                                    DEBUG)
                    do_report(averages, flock, fdata)

                waittime = sampletime - (time.time() -
                                         starttime) - (starttime % sampletime)
                if (waittime > 0):
                    mf.syslog_trace("Waiting  : {0}s".format(waittime), False,
                                    DEBUG)
                    mf.syslog_trace("................................", False,
                                    DEBUG)
                    time.sleep(waittime)
            except ValueError:
                mf.syslog_trace("Waiting for S.M.A.R.T. data..",
                                syslog.LOG_DEBUG, DEBUG)
                time.sleep(60)
                pass
            except Exception:
                mf.syslog_trace("Unexpected error in run()", syslog.LOG_CRIT,
                                DEBUG)
                mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
                raise
Exemplo n.º 22
0
    def run():
        iniconf = configparser.ConfigParser()
        inisection = MYID
        home = os.path.expanduser('~')
        s = iniconf.read(home + '/' + MYAPP + '/config.ini')
        mf.syslog_trace("Config file   : {0}".format(s), False, DEBUG)
        mf.syslog_trace(
            "Options       : {0}".format(iniconf.items(inisection)), False,
            DEBUG)
        reporttime = iniconf.getint(inisection, "reporttime")
        cycles = iniconf.getint(inisection, "cycles")
        samplespercycle = iniconf.getint(inisection, "samplespercycle")
        flock = iniconf.get(inisection, "lockfile")
        fdata = iniconf.get(inisection, "resultfile")

        samples = samplespercycle * cycles  # total number of samples averaged
        sampletime = reporttime / samplespercycle  # time [s] between samples

        data = []  # array for holding sampledata

        try:
            hwdevice = iniconf.get(inisection, NODE + ".hwdevice")
        except configparser.NoOptionError:  # no hwdevice
            mf.syslog_trace(traceback.format_exc(), False, DEBUG)
            sys.exit(0)
        if not os.path.isfile(hwdevice):
            mf.syslog_trace("** Device not found: {0}".format(hwdevice),
                            syslog.LOG_INFO, DEBUG)
            sys.exit(1)

        while True:
            try:
                starttime = time.time()

                result = do_work(hwdevice)
                mf.syslog_trace("Result   : {0}".format(result), False, DEBUG)

                data.append(float(result))
                if (len(data) > samples):
                    data.pop(0)
                mf.syslog_trace("Data     : {0}".format(data), False, DEBUG)

                # report sample average
                if (starttime % reporttime < sampletime):
                    averages = format(sum(data[:]) / len(data), '.3f')
                    mf.syslog_trace("Averages : {0}".format(averages), False,
                                    DEBUG)
                    do_report(averages, flock, fdata)

                waittime = sampletime - (time.time() -
                                         starttime) - (starttime % sampletime)
                if (waittime > 0):
                    mf.syslog_trace("Waiting  : {0}s".format(waittime), False,
                                    DEBUG)
                    mf.syslog_trace("................................", False,
                                    DEBUG)
                    time.sleep(waittime)
            except Exception:
                mf.syslog_trace("Unexpected error in run()", syslog.LOG_CRIT,
                                DEBUG)
                mf.syslog_trace(traceback.format_exc(), syslog.LOG_CRIT, DEBUG)
                raise