#!/bin/bash
# script to write static system data and upload to perflog.nordugrid.org
# together with performance data taken by data, arex, infosys and backends
# runs once a day, run by a-rex when configured through the helper option
# in the grid-manager block in arc.conf.
# e.g.:
# [grid-manager]
# helper=". /usr/share/arc/perferator"
# Path to arc.conf can be given with --config option, default is /etc/arc.conf
# TODO: Upload performance data to perflog.nordugrid.org

command_exists () {
  type "$1" &> /dev/null ;
}

write_static_system_data () {
  outfile="$1"
  echo "=== Timestamp: ===" >> $outfile
  date >> $outfile
  echo "" >> $outfile
  echo "=== ARC version: ===" >> $outfile
  arched --version >> $outfile
  echo "" >> $outfile
  echo "=== fs types: ===" >> $outfile
  controldir_fstype=`df --output=fstype $CONFIG_controldir | grep -v Type`
  echo "controldir fstype: $controldir_fstype" >> $outfile
  sessiondir_fstype=`df --output=fstype $CONFIG_sessiondir | grep -v Type`
  echo "sessiondir fstype: $sessiondir_fstype" >> $outfile
  echo "" >> $outfile
  echo "=== CPU info: ===" >> $outfile
  echo "no. of CPUs: `getconf _NPROCESSORS_ONLN`" >> $outfile
  cat /proc/cpuinfo >> $outfile
  echo "" >> $outfile
  echo "=== Mem info: ===" >> $outfile
  cat /proc/meminfo >> $outfile
  echo "" >> $outfile
  echo "=== OS info: ===" >> $outfile
  uname -a >> $outfile
  cat /etc/*-release >> $outfile
  cat /proc/version >> $outfile
  echo "" >> $outfile
}

# ARC1 passes first the config file.
if [ "$1" = "--config" ]; then shift; ARC_CONFIG=$1; shift; fi

basedir=`dirname $0`
basedir=`cd $basedir > /dev/null && pwd` || exit $?

pkglibexecdir="${ARC_LOCATION:-/cvmfs/dirac.egi.eu/dirac/v9.0.0a33/Linux-x86_64}/libexec/arc"
pkgdatadir="$basedir"

arcconfig_parser=${pkglibexecdir}/arcconfig-parser

ARC_CONFIG=${ARC_CONFIG:-/etc/arc.conf}

eval $(${arcconfig_parser} -c ${ARC_CONFIG} -b common -e bash)
eval $(${arcconfig_parser} -c ${ARC_CONFIG} -b gridmanager -e bash)

HOSTNAME=$CONFIG_hostname
PERFDIR=${CONFIG_perflogdir:-/var/log/arc/perfdata}

# sleep a bit, waiting for performance data to accumulate
sleep 86400

# merge infosys files
MERGEDATE=`date +%Y%m%d`

write_static_system_data $PERFDIR/sysinfo.perflog

# extract infosys data from nytprofd databases
$pkgdatadir/PerfData.pl --config=$ARC_CONFIG || exit $?