Пример #1
0
int cmd_session_list (int argc, const char *argv[]) {
    var *v = api_get ("/session");

    if (OPTIONS.json) {
        var_dump (v, stdout);
        var_free (v);
        return 0;
    }
    
    printf ("---------------------------------------------"
            "-----------------------------------\n");
    printf ("Session ID         Sender"
            "                                 Last Refresh\n");
    
    var *v_session = var_get_array_forkey (v, "session");
    var *crsr = v_session->value.arr.first;
    while (crsr) {
        printf ("%08x-%08x %-39s %s\n",
                (uint32_t) (var_get_int_forkey (crsr, "sessid")),
                (uint32_t) (var_get_int_forkey (crsr, "addr")),
                var_get_str_forkey (crsr, "remote"),
                var_get_str_forkey (crsr, "lastcycle"));
        crsr = crsr->next;
    }

    printf ("---------------------------------------------"
            "-----------------------------------\n");

    var_free (v);
    return 0;
}
Пример #2
0
/** The tenant-list command */
int cmd_tenant_list (int argc, const char *argv[]) {
    var *res = api_get ("/");
    if (OPTIONS.json) {
        var_dump (res, stdout);
    }
    else {
        printf ("UUID                                 Hosts  Name\n");
        printf ("---------------------------------------------"
                "-----------------------------------\n");

        var *res_tenant = var_get_array_forkey (res, "tenant");
        if (var_get_count (res_tenant)) {
            var *crsr = res_tenant->value.arr.first;
            while (crsr) {
                printf ("%s %5llu  %s\n",
                        var_get_str_forkey (crsr, "id"),
                        var_get_int_forkey (crsr, "hostcount"),
                        var_get_str_forkey (crsr, "name"));
                crsr = crsr->next;
            }
        }
        printf ("----------------------------------------------"
                "----------------------------------\n");
    }
    var_free (res);
    return 0;
}
Пример #3
0
int cmd_host_overview (int argc, const char *argv[]) {
    if (OPTIONS.tenant[0] == 0) {
        fprintf (stderr, "%% No tenantid provided\n");
        return 1;
    }

    var *ov = api_get ("/%s/host/overview", OPTIONS.tenant);
    if (! var_get_count (ov)) return 0;

    printf ("Name                            Status     "
            "Load  Net i/o      CPU\n");
    printf ("---------------------------------------------"
            "-----------------------------------\n");
    
    var *ov_dict = var_get_dict_forkey (ov, "overview");
    if (! var_get_count (ov_dict)) return 0;
    var *crsr = ov_dict->value.arr.first;
    while (crsr) {
        const char *hname = var_get_str_forkey (crsr, "hostname");
        if (! hname) hname = "";
        char shortname[32];
        strncpy (shortname, hname, 31);
        shortname[31] = 0;
        const char *hstat = var_get_str_forkey (crsr, "status");
        if (! hstat ) hstat = "UNSET";
        double load = var_get_double_forkey (crsr, "loadavg");
        uint64_t netio = var_get_int_forkey (crsr, "net/in_kbs");
        netio += var_get_int_forkey (crsr, "net/out_kbs");
        double cpu = var_get_double_forkey (crsr, "pcpu");
        int rcpu = (cpu+5.0) / 10;
        printf ("%-31s %-8s %6.2f %8llu %6.2f %% -[",
                shortname, hstat, load, netio, cpu);
        for (int i=0; i<10; i++) {
            if (i< rcpu) printf ("#");
            else printf (" ");
        }
        printf ("]+\n");
        crsr = crsr->next;
    }
    var_free (ov);
    printf ("---------------------------------------------"
            "-----------------------------------\n");
    return 0;
}    
Пример #4
0
/** The host-list command */
int cmd_host_list (int argc, const char *argv[]) {
    if (OPTIONS.tenant[0] == 0) {
        fprintf (stderr, "%% No tenantid provided\n");
        return 1;
    }

    const char *unit;
    
    var *apires = api_get ("/%s/host", OPTIONS.tenant);
    
    if (OPTIONS.json) {
        var_dump (apires, stdout);
        var_free (apires);
        return 0;
    }
    
    var *v_hosts = var_get_array_forkey (apires, "host");
    if (var_get_count (v_hosts)) {
        printf ("UUID                                    Size "
                "First record      Last record\n");
        printf ("---------------------------------------------"
                "-----------------------------------\n");
        var *crsr = v_hosts->value.arr.first;
        
        while (crsr) {
            uint64_t usage = var_get_int_forkey (crsr, "usage");
            unit = "KB";
            usage = usage / 1024;
            if (usage > 2048) {
                unit = "MB";
                usage = usage / 1024;
            }
            
            char start[24];
            char end[24];
            
            strncpy (start, var_get_str_forkey (crsr, "start"), 23);
            strncpy (end, var_get_str_forkey (crsr, "end"), 23);
            start[16] = 0;
            end[16] = 0;
            start[10] = ' ';
            end[10] = ' ';

            printf ("%s %4llu %s %s  %s\n",
                    var_get_str_forkey (crsr, "id"),
                    usage, unit, start, end);
            crsr = crsr->next;
        }
        printf ("---------------------------------------------"
                "-----------------------------------\n");
    }

    var_free (apires);
    return 0;
}
Пример #5
0
/** Parse /meter into probes */
int conf_probe (const char *id, var *v, updatetype tp) {
    if (tp != UPDATE_ADD) exit (0);
    const char *vtp = var_get_str_forkey (v, "type");
    const char *call = var_get_str_forkey (v, "call");
    int interval = var_get_int_forkey (v, "interval");
    probetype t = PROBE_BUILTIN;
    
    if (vtp && call && interval) {
        if (strcmp (vtp, "exec") == 0) t = PROBE_EXEC;
        else if (strcmp (vtp, "nagios") == 0) t = PROBE_NAGIOS;
        if (probelist_add (&APP.probes, t, call, id, interval)) {
            return 1;
        }
        else {
            log_error ("Error adding probe-call <%s>", call);
        }
    }
    return 0;
}
Пример #6
0
int main (int argc, const char *argv[]) {
    const char *tstr = NULL;
    var *env = var_alloc();
    var *env_collector = var_get_dict_forkey (env, "collector");
    var *env_colors = var_get_array_forkey (env, "colors");
    var_set_int_forkey (env_collector, "listenport", 3333);
    var_set_str_forkey (env_collector, "address", "127.0.0.1");
    var_set_str_forkey (env_collector, "key", "secret");
    var_clear_array (env_colors);
    var_add_str (env_colors, "red");
    var_add_str (env_colors, "green");
    var_add_str (env_colors, "blue");
    
    assert (var_get_int_forkey (env_collector, "listenport") == 3333);
    assert (tstr = var_get_str_forkey (env_collector, "address"));
    assert (strcmp (tstr, "127.0.0.1") == 0);
    assert (tstr = var_get_str_forkey (env_collector, "key"));
    assert (strcmp (tstr, "secret") == 0);
    assert (var_get_count (env_collector) == 3);
    assert (tstr = var_get_str_atindex (env_colors, 0));
    assert (strcmp (tstr, "red") == 0);
    assert (tstr = var_get_str_atindex (env_colors, 1));
    assert (strcmp (tstr, "green") == 0);
    assert (tstr = var_get_str_atindex (env_colors, 2));
    assert (strcmp (tstr, "blue") == 0);
    
    time_t tnow = time (NULL);
    var_set_time_forkey (env, "nowtime", tnow);
    assert (var_get_time_forkey (env, "nowtime") == tnow);
    var_set_unixtime_forkey (env, "unixtime", tnow);
    assert (var_get_time_forkey (env, "unixtime") == tnow);
    
    uuid uuid_in = uuidgen();
    var_set_uuid_forkey (env, "myuuid", uuid_in);
    uuid uuid_out = var_get_uuid_forkey (env, "myuuid");
    assert (uuidcmp (uuid_in, uuid_out));
    
    var_new_generation (env);
    var_set_int_forkey (env_collector, "listenport", 3333);
    var_set_str_forkey (env_collector, "address", "192.168.1.1");
    var_clear_array (env_colors);
    var_add_str (env_colors, "red");
    var_add_str (env_colors, "green");
    var_add_str (env_colors, "blue");
    
    var *lport, *addr, *key;
    lport = var_find_key (env_collector, "listenport");
    addr = var_find_key (env_collector, "address");
    key = var_find_key (env_collector, "key");
    assert (lport);
    assert (addr);
    assert (key);
    
    /* collector.listenport should be unchanged from first generation */
    assert (lport->generation == env->generation);
    assert (lport->lastmodified < env->generation);
    assert (lport->firstseen == lport->lastmodified);
    
    /* collector.addr should be changed this generation */
    assert (addr->generation == env->generation);
    assert (addr->lastmodified == env->generation);
    assert (addr->firstseen == env->firstseen);
    
    /* colors should be changed this generation */
    assert (env_colors->generation == env->generation);
    assert (env_colors->lastmodified == env->generation);
    assert (env_colors->firstseen == env->firstseen);
    
    /* collector.key should be stale */
    assert (key->generation < env->generation);
    
    var_clean_generation (env);
    
    /* collector.key should now be deleted */
    key = var_find_key (env_collector, "key");
    assert (key == NULL);
    return 0;
}
Пример #7
0
/** Daemon main run loop. */
int daemon_main (int argc, const char *argv[]) {
    const char *buf;
    size_t sz;

    if (strcmp (APP.logpath, "@syslog") == 0) {
        log_open_syslog ("opticon-agent", APP.loglevel);
    }
    else {
        log_open_file (APP.logpath, APP.loglevel);
    }
    
    probelist_start (&APP.probes);
    APP.resender = authresender_create (APP.transport);
    
    time_t tlast = time (NULL);
    time_t nextslow = tlast + 5;
    time_t nextsend = tlast + 10;
    time_t lastkeyrotate = 0;
    int slowround = 0;

    log_info ("Daemonized");
    while (1) {
        time_t tnow = tlast = time (NULL);
        
        slowround = 0;
        
        /* If a slow round is due at this time, use the excuse to send an
           authentication packet */
        if (nextslow <= tnow) {
            slowround = 1;
            uint32_t sid = APP.auth.sessionid;
            if (! sid) sid = gen_sessionid();
            log_debug ("Authenticating session <%08x>", sid);
            APP.auth.sessionid = sid;
            APP.auth.serial = 0;
            APP.auth.tenantid = APP.tenantid;
            APP.auth.hostid = APP.hostid;
            
            /* Only rotate the AES key every half hour */
            if (tnow - lastkeyrotate > 1800) {
                APP.auth.sessionkey = aeskey_create();
                lastkeyrotate = tnow;
            }
            APP.auth.tenantkey = APP.collectorkey;
            
            /* Dispatch */
            ioport *io_authpkt = ioport_wrap_authdata (&APP.auth,
                                                       gen_serial());
            
            sz = ioport_read_available (io_authpkt);
            buf = ioport_get_buffer (io_authpkt);
            outtransport_send (APP.transport, (void*) buf, sz);
            authresender_schedule (APP.resender, buf, sz);
            ioport_close (io_authpkt);
            
            /* Schedule next slow round */
            nextslow = nextslow + 300;
        }
        
        log_debug ("Poking probes");

        probe *p = APP.probes.first;
        time_t wakenext = tnow + 300;

        /* Go over the probes to figure out whether we should kick them */        
        while (p) {
            time_t firewhen = p->lastpulse + p->interval;
            if (firewhen <= tnow) {
                conditional_signal (&p->pulse);
                p->lastpulse = tnow;
            }
            
            /* Figure out whether the next event for this probe is sooner
               than the next wake-up time we determined so far */
            if (p->lastpulse + p->interval < wakenext) {
                wakenext = p->lastpulse + p->interval;
            }
            
            p = p->next;
        }
        
        int collected = 0;
        int ncollected = 0;
        host *h = host_alloc();
        var *vnagios = var_alloc();
        var *vchkwarn = var_get_array_forkey (vnagios, "chkwarn");
        var *vchkalert = var_get_array_forkey (vnagios, "chkalert");
        
        /* If we're in a slow round, we already know we're scheduled. Otherwise,
           see if the next scheduled moment for sending a (fast lane) packet
           has passed. */
        if (slowround || (tnow >= nextsend)) {
            h->uuid = APP.hostid;
            host_begin_update (h, time (NULL));

            if (! slowround) while (nextsend <= tnow) nextsend += 60;
            log_debug ("Collecting probes");
        
            /* Go over the probes again, picking up the ones relevant to the
               current round being performed */
            p = APP.probes.first;
            while (p) {
                pthread_mutex_lock (&p->vlock);
                volatile var *v = p->vcurrent;
                /* See if data for this probe has been collected since the last kick */
                if (v && (p->lastdispatch <= p->lastreply)) {
                    /* Filter probes for the current lane */
                    if ((slowround && p->interval>60) ||
                        ((!slowround) && p->interval<61)) {
                        log_debug ("Collecting <%s>", p->call);
                        
                        if (p->type == PROBE_NAGIOS) {
                            /* Check for alert/warning state and
                             * summarize before adding to host struct */
                             int pstatus = var_get_int_forkey ((var*)v, "status");
                             if (pstatus) {
                                 var *arr;
                                 switch (pstatus) {
                                    case 1:
                                        var_add_str (vchkwarn, p->id);
                                        break;
                                    
                                    default:
                                        var_add_str (vchkalert, p->id);
                                        break;
                                }
                                collected++;
                            }
                            ncollected++;
                        }
                        else {
                            host_import (h, (var *) v);
                            collected++;
                        }
                        p->lastdispatch = tnow;
                    }
                }
                else {
                    if (tnow - p->lastreply > (2*(p->interval))) {
                        log_warn ("Probe <%s> seems stuck after %i seconds",
                                  p->call, tnow - p->lastreply);
                    }
                }
                pthread_mutex_unlock (&p->vlock);
                p = p->next;
            }
        }
        
        /* Add the chk tree with nagios self-checks to the data */
        if (ncollected) host_import (h, vnagios);
         
        /* If any data was collected, encode it */
        if (collected) {
            log_debug ("Encoding probes");
        
            ioport *encoded = ioport_create_buffer (NULL, 4096);
            if (! encoded) {
                log_warn ("Error creating ioport");
                ioport_close (encoded);
                host_delete (h);
                continue;
            }
        
            if (! codec_encode_host (APP.codec, encoded, h)) {
                log_warn ("Error encoding host");
                ioport_close (encoded);
                host_delete (h);
                continue;
            }

            if (APP.dumppath) {
                FILE *pktf = fopen (APP.dumppath,"a");
                fprintf (pktf, "\n--- %s ---\n\n", slowround?"Slow":"Fast");
                ioport *dump = ioport_create_filewriter (pktf);
                log_debug ("dump %llx", dump);
                codec *jsonc = codec_create_json();
                log_debug ("jsonc %llx", jsonc);
                codec_encode_host (jsonc, dump, h);
                codec_release (jsonc);
                ioport_close (dump);
                fclose (pktf);
            }
        
            log_debug ("Encoded %i bytes", ioport_read_available (encoded));

            ioport *wrapped = ioport_wrap_meterdata (APP.auth.sessionid,
                                                     gen_serial(),
                                                     APP.auth.sessionkey,
                                                     encoded);
        
        
            if (! wrapped) {
                log_error ("Error wrapping");
                ioport_close (encoded);
                host_delete (h);
                continue;
            }
        
            sz = ioport_read_available (wrapped);
            buf = ioport_get_buffer (wrapped);
        
            /* Send it off into space */
            outtransport_send (APP.transport, (void*) buf, sz);
            log_info ("%s lane packet sent: %i bytes", 
                      slowround ? "Slow":"Fast", sz);

            ioport_close (wrapped);
            ioport_close (encoded);
        }
        
        /* Done with the host object */
        host_delete (h);

        /* Figure out what the next scheduled wake-up time is */
        tnow = time (NULL);
        if (nextsend < wakenext) wakenext = nextsend;
        if (nextslow < wakenext) wakenext = nextslow;
        if (wakenext > tnow) {
            log_debug ("Sleeping for %i seconds", (wakenext-tnow));
            sleep (wakenext-tnow);
        }
    }
    return 666;
}