#
# Procs monitor

test procs_PROCS {
    static set name "procs";
    static set domain procs;
    static set procsvariable "hrSWRunName";
    static set features unix local;

    static set description "Monitors running processes";
    pernode set report_item "procs";
    set perf_frequency 0;

    static set arguments
	"proc:string:name of a monitored process"
	"min:int:minimum number of processes of that name that must run (default:1)"
	"max:int:maximum number of processes of that name that may run (default: infinite)"
	"pscomm:string:ps command to use (default: system dependent)"
	"item:string:column name we should report the results under"
	"perf:int:time in minutes between sending performance data (0 for no performance data)";

    set proc none;
    set min 1;
    set max 1000000;

    init {
        instance import proc;
	instance import min;
	instance import max;
	pernode import pscomm;
	instance import item report_item;
	instance import perf perf_frequency;
    }

    discover {
        debug 3 monitoring process ${proc} (min: ${min}, max: ${max});
    }

    pernode monitor {
        get @${domain}.${procsvariable};
	pernode set proctable ${${domain}.${procsvariable}};
	pernode set countprocs call( procs::countprocs , ${proctable} );
    }

    report {
	instance set ourproc call( procs::selectproc, ${countprocs}, ${proc} );
	debug 5 process ${proc} : ${ourproc} instances;
	status red ( ${ourproc} < ${min} ) && ( ${ourproc} == 0 ) "&red " ${proc} " (no process)";
        status red ( ${ourproc} < ${min} ) && ( ${ourproc} > 0 ) "&red " ${proc} " (too few (" ${ourproc} ") instances)";
        status red ${ourproc} > ${max} "&red " ${proc} "(too many (" ${ourproc} ") instances)";
        status green 1 "&green " ${proc} " (" ${ourproc} " instances)";
    }

    pernode perf {
        set count size( ${proctable} );
        export always procs.nrProcesses, ${count};
    }

    perf {
        export always procs.proc.${proc}.count, ${ourproc};
        export sometimes procs.proc.${proc}.name, ${proc};
    }
}
        



# under Solaris we use "ps cax"
test procs_BSDProcs extends procs_PROCS {
    static set features unix bsd local;
    static set pscomm "ps cax";
}

# same for MacOS
test procs_macProcs extends procs_BSDProcs {
    static set features macos local;
}


# under Solaris we use /usr/ucb/ps
test procs_SolarisProcs extends procs_PROCS {
    static set features local unix sysv solaris;
    static set pscomm "/usr/ucb/ps -cax";
}



# under Linux we use ps -e
test procs_LinuxProcs extends procs_PROCS {
    static set features local unix linux;
    static set pscomm "ps -e";
}


# under SystemV we try "ps -e -opid -ocomm"
test procs_sysvProcs extends procs_PROCS {
    static set features local unix sysv;
    static set pscomm "ps -e -opid -ocomm";
}


# ... same for Tru64
test procs_true64Procs extends procs_sysvProcs {
    static set features local unix tru64;
}

# ... same for HP/UX
test procs_true64Procs extends procs_sysvProcs {
    static set features local unix hpux;
}

