#!/usr/bin/ruby -w
#
# Copyright (c) 2006 Meraki Networks, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, subject to the conditions
# listed in the Meraki.LICENSE file. These conditions include: you must
# preserve this copyright notice, and you cannot mention the copyright
# holders in advertising related to the Software without their permission.
# The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
# notice is a summary of the Meraki.LICENSE file; the license in that file is
# legally binding.
#
#

tickInterval = 60
watchdogInterval = 10

lowMemLimit = 7*1024 # in kB

lastTick = Time.now

$ticksLowMem = 0
$ticksNoClick = 0
$ticksNoBrain = 0
$ticksNoGateway = 0
$ticksUpgrading = 0
$ticksWatchdogRunning = 0

def dumpState(file)
  fileio = open(file, "w")
  fileio.puts("ticksLowMem: #{$ticksLowMem}")
  fileio.puts("ticksNoClick: #{$ticksNoClick}")
  fileio.puts("ticksNoBrain: #{$ticksNoBrain}")
  fileio.puts("ticksNoGateway: #{$ticksNoGateway}")
  fileio.puts("ticksUpgrading: #{$ticksUpgrading}")
  fileio.puts("ticksWatchdogRunning: #{$ticksWatchdogRunning}")
  fileio.close
end

def truncLog(file)
  begin
    if File.exists?(file) && File.size(file) > 200*1024
      File.truncate(file, 100*1024)
    end
  rescue
  end
end

loop do
  if (Time.now - lastTick > tickInterval)
    lastTick = Time.now
    $ticksLowMem += 1
    $ticksNoClick += 1
    $ticksNoBrain += 1
    $ticksNoGateway += 1
    $ticksUpgrading += 1
    $ticksWatchdogRunning += 1

    psout = `ps`
    meminfo = `cat /proc/meminfo`
    memFree = meminfo[/MemFree:\s+(\d+)/, 1].to_i

    if (memFree > lowMemLimit)
      $ticksLowMem = 0
    end
    if psout.match("/usr/bin/click")
      $ticksNoClick = 0
    end
    if psout.match("/usr/bin/brain")
      $ticksNoBrain = 0
    end
    if system("ping -c 1 18.26.4.9 > /dev/null")
      $ticksNoGateway = 0
    end
    if psout.match("/usr/bin/brain")
      $ticksNoBrain = 0
    end
    if !File.exists?("/tmp/upgrade_in_progress")
      $ticksUpgrading = 0
    end

    truncLog("/tmp/brain.log")
    truncLog("/tmp/error.log")
    truncLog("/tmp/eth0.dhcplog")
    truncLog("/tmp/sr2.log")

    dumpState("/tmp/meraki-watchdog.status")
  end

  if (($ticksWatchdogRunning < 5) ||
      (($ticksUpgrading > 0) && ($ticksUpgrading < 40)) ||
      (($ticksLowMem < 15) &&
       ($ticksNoClick < 5) &&
       ($ticksNoBrain < 5) &&
       ($ticksNoGateway < 240) &&
       ($ticksUpgrading < 120) &&
       ($ticksWatchdogRunning < (12*24*60))))
  else
    dumpState("/storage/meraki-watchdog.rebootstatus")
    system("/sbin/reboot")
  end

  sleep(watchdogInterval)
end
