#!/bin/bash

cd `dirname $0`

#get the host's ip address
#ip=`ifconfig |grep -w inet|grep Bcast|awk -F '[:]' '{print $2}'|awk '{print $1}'`
hostname=`hostname`
function show_elapsed_time()
{
        user_hz=$(getconf CLK_TCK) #mostly it's 100 on x86/x86_64
        pid=$1
        if [ -z $pid ]; then
                echo "0"
        else
                jiffies=$(cat /proc/$pid/stat | cut -d" " -f22)
                sys_uptime=$(cat /proc/uptime | cut -d" " -f1)
                last_time=$(( ${sys_uptime%.*} - $jiffies/$user_hz ))
                echo "${last_time}"
        fi
}

while true
do
  cat  process_monitor.list|while read line
  do
  pname=`echo $line|awk -F\| '{print $1}' `
  pkeyword=`echo $line|awk -F\| '{$1="";print $0}' |sed "s/ //"`
  pid=$(ps -ef| grep "${pkeyword}" |grep -v "$0"| grep -v grep|awk '{print $2}')
  runtime=`show_elapsed_time $pid`
  echo "${pname}_process_runtime ${runtime}" |curl --data-binary @- http://10.20.15.17:9091/metrics/job/${hostname}
  done
  sleep 30
done
