test etherport_check {
    # these are defaults describing some test parameters
    static set features remote;
    static set name etherport;

    # re-discover every 60 minutes
    set discover_refresh 60;
    # after a discover failure retry every 10 minutes
    set discover_retry 10;
    # monitor every 5 minutes by default
    set frequency 5;
    # report as item "port"
    pernode set report_item "port";
    set host "set by uxmon";
    set alias "set by uxmon";
    # do not send perf variables by default
    set perf_frequency 0;

    # the module we get our configuration from
    static set domain snmp;

    # if interface contains a list of interfaces we will only monitor those
    set interface;
    set ignoreadmin 1;
    # report yellow if errors go above this rate
    set community "public";
    # interfac names which are not reported
    static set avoid_interfaces;

    # just declare a few variables here
    set thismonitor;

    pernode set monitored;
    pernode set formerly_monitored;
    pernode set former_description;

    static set description
	"Monitor interface operating status";

    static set arguments
	"interface:@pattern:A list of interface names"
	"ignoreadmin:bool:If true also monitor interfaces that are disabled by configuration (default: ${ignoreadmin})"
	"community:string:The SNMP get community"
	"item:string:column name we should report the results under (default: cpu)"
	"perf:int:time in minutes between sending performance data (0 for no performance data)";

    # import configuration from uxmon-net
    init {
	debug 3 "etherport test starting";
	instance import interface;
	instance import community;
	instance import perf perf_frequency;
	instance import ignoreadmin;
	pernode import item report_item;
    }

    # walk the relevant data structures and do some pre-processing
    pernode discover {
	debug 3 "discovering network interfaces";
	get @${domain}.ifType @${domain}.ifAdminStatus @${domain}.ifDescr;
	debug 4 "got ${${domain}.ifDescr}";
	pernode set iface.description ${${domain}.ifDescr};
	pernode set iface.type ${${domain}.ifType};
	pernode set iface.adminstatus ${${domain}.ifAdminStatus};

    }

    # select the entities this instance will monitor
    discover {
	debug 4 "selecting interfaces";
	set oldmonitor ${thismonitor};
	set monitored remove( ${monitored}, ${oldmonitor} );

	set index ${iface.description};
	select thismonitor index
	    ( null( ${interface} ) || contains_pattern( ${interface}, ${iface.description[${i}]} ) )
	   && not( contains( ${monitored}, ${i} ) )
	   && ( ( ${iface.adminstatus[${i}]} == 1 ) || ${ignoreadmin} );

	set monitored ${monitored} ${thismonitor};
    };

    pernode postdiscover {
	debug 3 "currently monitored:" ${monitored};
	set notmonitored_anymore unmonitored( ${formerly_monitored}, ${monitored}, ${former_description} );
	set newly_monitored nowmonitored( ${formerly_monitored}, ${monitored}, ${iface.description} );
	event "notice" ${notmonitored_anymore};
	event "notice" ${newly_monitored};
	pernode set formerly_monitored ${monitored};
	pernode set former_description ${iface.description};
    }

    pernode monitor {
	# clear variables
	pernode set iface.operstatus;
    }

    monitor {
	debug 3 getting ${thismonitor} for domain ${domain};
	get ${domain}.ifOperStatus[${thismonitor}];

	for iface.operstatus thismonitor ${${domain}.ifOperStatus[${i}]};
    };

    pernode report {
	debug 3 monitored items are ${monitored};
	set text;
	status purple null( ${monitored} ) "no interfaces monitored";
    };

    report for thismonitor {
	status red not( null( ${iface.operstatus[${p}]} ) ) && not( ${iface.operstatus[${p}]} == 1 )
	    "&red " ${iface.description[${p}]} " is down";
	status green 1
	    "&green" " " ${iface.description[${p}]} " ok";
    };

    perf {
        export always iface.operstatus, ${iface.operstatus[${thismonitor}]};
	export sometimes iface.description, ${iface.description[${thismonitor}]};
    };

    pernode unconfigure {
	pernode set monitored;
	pernode set iface.description;
	pernode set iface.speed;
	pernode set iface.type;
	pernode set iface.adminstatus;
	pernode set monitored;
    }
}



