#
# TCP monitor

test tcp_TCP {
    static set name "tcp";
    static set domain tcp;

    static set description "Monitors a generic TCP based service";

    set niceness 5;

    static set arguments
	"port:int|string:port number or port name uxmon will try to connect to"
	"service:string:name of the service uxmon expects to be run behind the port (e.g. 'ftp', 'pop3')"
	"item:string:column name we should report the results under"
	"perf:int:time in minutes between sending performance data (0 for no performance data)";

    init {
        instance import port;
	instance import service;
	instance set service if( ${service}, ${service}, ${port} );
	instance set port if( ${port}, ${port}, ${service} );
	instance import item report_item;
	instance set report_item if( ${report_item}, ${report_item}, ${service} );
	instance set id 1;
	instance import perf perf_frequency;
    }

    discover {
        debug 3 monitoring TCP port ${port} ", service" ${service} " => " ${report_item};
    }

    monitor {
	debug 6 "domain: " ${domain} ", port: " ${port} ", service: " ${service};
        set var ${domain}.${port}.${service};
        debug 6 get ${var}.status.${id} ${var}.message.${id} ${var}.time.${id};
        get ${var}.status.${id} ${var}.message.${id} ${var}.time.${id} ${var}.log.${id};
	instance set status ${${var}.status.${id}};
	instance set message ${${var}.message.${id}};
	instance set time ${${var}.time.${id}};
	instance set log ${${var}.log.${id}};
    }

    report {
        set text ${service} " " ${message} " (" ${time} "s)";
        status red ${status} == 0 "&red " ${text};
        status yellow ${status} == 1 "&yellow " ${text};
        status purple ${status} == 2 "&purple " ${text};
        status green ${status} == 3 "&green " ${text};
	comment "html" "<HR></HR><PRE><strong>" ${service} " (" ${port} ") session data\n\n" </strong> ${log} "</PRE>";
    }

    perf {
        export always tcp.${service}.time, ${time};
	export sometimes tcp.${service}.name, ${service};
    }
}
        

    
#
# TCP class tests
#

#
# test http server
#
test tcp_http extends tcp_TCP {
    static set name "http";
    pernode set seq 1 - 1;
    set port http;
    set service http;

    static set description "Monitors a http server";
    static set arguments
        "url:string:URL uxmon tries to load in order to determine if the server is up"
	"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)";

    init {
	instance import url spec1;
	set seq ${seq} + 1;
	instance set id ${seq};
    }

    postmonitor {
	set log reference_url( ${log}, "s#>> HEAD (.*) HTTP#>> HEAD <A HREF='<URL>'>\\$1</A> HTTP#", ${host}, ${spec1}, ${port}, ${service} );
    }
}

#
# demonstrate how we can specify a chat script without having to alter the
# perl code
#
test tcp_ident extends tcp_TCP {
    static set name "ident";
    static set description "Monitors ident server";
    static set arguments;

    set port ident;
    set service ident;
    set chat "0,0\r\n" ".*:.*:" "quit";
}
    


test tcp_short extends tcp_TCP {
    static set description "Monitor SNMP service. Shortcut for: service=smtp tcp";
    static set name smtp;
    set port smtp;
    set service smtp;
}
test tcp_telnet extends tcp_TCP {
    static set description "Monitor telnet service. Shortcut for: service=telnet tcp";
    static set name telnet;
    set port telnet;
    set service telnet;
}
test tcp_ssh extends tcp_TCP {
    static set description "Monitor SSH service. Shortcut for: service=ssh tcp";
    static set name ssh;
    set port ssh;
    set service ssh;
}
test tcp_pop3 extends tcp_TCP {
    static set description "Monitor POP3 service. Shortcut for: service=pop3 tcp";
    static set name pop3;
    set port pop3;
    set service pop3;
}
test tcp_imap extends tcp_TCP {
    static set description "Monitor IMAP service. Shortcut for: service=imap tcp";
    static set name imap;
    set port imap;
    set service imap;
}
test tcp_nntp extends tcp_TCP {
    static set description "Monitor NNTP Service. Shortcut for: service=nntp tcp";
    static set name nntp;
    set port nntp;
    set service nntp;
}
test tcp_ica extends tcp_TCP {
    static set description "Monitor Citrix ICA service. Shortcut for: service=ica tcp";
    static set name ica;
    set port ica;
    set service ica;
}
test tcp_ftp extends tcp_TCP {
    static set description "Monitor FTP service. Shortcut for: service=ftp tcp";
    static set name ftp;
    set port ftp;
    set service ftp;
}
