I'd like some input on method usage.

>From the documentation it seems methods are expected to be entirely
self-contained. I haven't seen any examples of using methods relating to
other promises. I think bundles that can be used like this provide an
easy way to share configurations.

Two bundles are attached in check_http.cf.

check_http_content searches the content of a page for an expected string
and define a class if its found.

check_http_status is a similar bundle that checks the status returned
from a web-server.

An example of how they could be used.

bundle agent main {
    methods:
        "any" usebundle => check_http_content("www.google.com", "80",
"/", "google_has_something_new", "New!");

    reports:
        google_has_something_new::
            "There is something new at google!";
}

example output from a run.
cf3>     .........................................................
cf3>     Promise handle:
cf3>     Promise made by: There is something new at google!
cf3>     .........................................................
cf3>
cf3> Report: There is something new at google!
cf3> R: There is something new at google!


This all seems to work fine for me. I am looking for some comments on
the general approach and some criticism of the bundles themselves. I
think that I need to change the check_http_status to accept a string for
"expected" status codes instead of hard coding only 200 in.

Is thee some native cfengine function that I could use in
check_http_content so as not to rely on curl being available?

-- 
Nick Anderson <n...@cmdln.org>
# Example Usage
# "any" usebundle => check_http_status("www.cmdln.org", "80", 
"/","cmdln_org_http_status_ok");
# "any" usebundle => check_http_content("www.cmdln.org", "80", 
"/","cmdln_org_http_content_ok", "a sysadmin blog");

bundle agent check_http_status(host,port,path,success_class) {
# Defines $(success_class) if check is successful
    vars:
        "status_ok" 
            comment => "HTTP Return code 200 is expected for OK status.",
            handle => "var_status_ok_in_bundle_agent_check_http_status",
            string => ".*200 OK.*\n.*";

        "request"   
           comment => "String to send to http server",
            handle => "var_request_in_bundle_agent_check_http_status",
            string => "GET $(path) HTTP/1.1 $(const.r)$(const.n)Host: 
$(host)$(const.r)$(const.n)$(const.r)$(const.n)";

        !http_status_ok::
            "retrieved_status"
                comment => "HTTP Status response from $(host):$(port) 
$(request)",
                 handle => 
"var_retrieved_status_in_bundle_agent_check_http_status",
                 string => readtcp("$(host)", "$(port)", "$(request)", 20);
        
    classes:
        "http_status_ok"
                comment => "define class if $(status_ok) matches 
$(retrieved_status)",
                 handle => 
"class_http_status_ok_in_bundle_agent_check_http_status",
             expression => regcmp("$(status_ok)", "$(retrieved_status)");

    commands:
        http_status_ok::
            "/bin/true"
                comment => "Define specific global class if http status OK. 
This can be useful for job control.",
                classes => if_repaired("$(success_class)");
    reports:
        http_status_ok::
            "Got $(retrieved_status) from $(host):$(port)$(path)"; 

        !http_status_ok::
            "Got $(retrieved_status) from $(host):$(port)$(path)"; 
        
}


bundle agent check_http_content (host, port, path, success_class, 
expected_content) {
# Defines $(success_class) if check is successful

    vars:
        "retrieved_content" 
             string => readfile( 
"/tmp/file_retrieved_content_in_bundle_agent_check_http_content_$(host)_$(success_class)"
 , "8000" );

    classes:
        "found_expected_content"
               comment => "Look for expected content in retrieved page",
                handle => 
"class_found_expected_content_in_bundle_agent_check_http_content",
            expression => regcmp(".*$(expected_content).*", 
"$(retrieved_content)");

    packages:
        ubuntu|debian::
            "curl"
                comment => "Make sure curl is installed, its needed for this 
test",
                package_policy => "add",
                package_method => apt;
        redhat::
            "curl"
                comment => "Make sure curl is installed, its needed for this 
test",
                package_policy => "add",
                package_method => yum;

    commands:
        found_expected_content::
            "/bin/true" 
                comment => "Define specific global class if expected content is 
found. This can be useful for job control.",
                classes => if_repaired("$(success_class)");

        !(found_expected_content|content_retrieved)::
            "/usr/bin/curl"
                comment => "Page content to parse", 
                 args => "--silent http://$(host):$(port)$(path) -o 
/tmp/file_retrieved_content_in_bundle_agent_check_http_content_$(host)_$(success_class)",
                 classes => if_repaired("content_retrieved");

    reports:
        found_expected_content::
            "Found expected content '$(expected_content)' in 
http://$(host):$(port)$(path) $(retrieved_content)";

}
_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to