//*******************************************************************
// RETRIEVE PRECEDING RAW RECORD FROM DATABASE (well just the color)
//*******************************************************************
static struct probe_result *get_previous_record(trx *t)
{
  dbi_result result;
  struct probe_result *prv;

  prv = g_malloc0(sizeof(struct probe_result));
  if (prv == NULL) {
    return(NULL);
  }
  prv->stattime = t->res->stattime;
  prv->color = t->res->color;

  if (!t->probe->store_results) return(prv); // we probably don't have a xxxx_raw table

  result = db_query(t->probe->db, 0,
                    "select   color, stattime "
                    "from     pr_%s_raw use index(probstat) "
                    "where    probe = '%u' and stattime < '%u' "
                    "order by stattime desc limit 1", 
                    t->res->name, t->def->probeid, t->res->stattime);
  if (!result) return(prv);
  if (dbi_result_next_row(result)) {
    prv->color = dbi_result_get_int(result, "color");
    prv->stattime = dbi_result_get_int(result, "stattime");
  }
  dbi_result_free(result);
  t->res->prevcolor = prv->color;
  return(prv);
}
Exemple #2
0
/////////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// Purpose:
//
// Parameters:
//
// Return value:
//
// Author: Komatsu Yuji(Zheng Chuyu)
//
/////////////////////////////////////////////////////////////////////////////////
int rootjobnet_load(rootjobnet_t * obj, dbi_result res)
{
    jbxlog_trace("In %s()", __func__);

    if (obj == NULL || res == NULL)
        return -1;

    rootjobnet_init(obj);

    obj->id = dbi_result_get_ulonglong(res, "id");
    obj->jobunit_id = dbi_result_get_ulonglong(res, "jobunit_id");
    obj->proc_jobunit_id =
        dbi_result_get_ulonglong(res, "proc_jobunit_id");
    obj->hist_jobunit_id =
        dbi_result_get_ulonglong(res, "hist_jobunit_id");

    obj->user_id = dbi_result_get_ulonglong(res, "user_id");
    obj->disabled = dbi_result_get_int(res, "disabled");
    obj->active_since = jbxdb_get_datetime(res, "active_since");
    obj->active_till = jbxdb_get_datetime(res, "active_till");
    obj->calendar_id = dbi_result_get_ulonglong(res, "calendar_id");
    obj->run_type = dbi_result_get_int(res, "run_type");
    obj->delay_limit = dbi_result_get_int(res, "delay_limit");
    obj->loader_interval = dbi_result_get_int(res, "loader_interval");

    return 0;
}
Exemple #3
0
/////////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// Purpose:
//
// Parameters:
//
// Return value:
//
// Author: Komatsu Yuji(Zheng Chuyu)
//
/////////////////////////////////////////////////////////////////////////////////
int winjob_load(winjob_t * obj, dbi_result res)
{
    jhklog_trace("In %s()", __func__);
    if (obj == NULL || res == NULL)
        return -1;

    winjob_init(obj);

    obj->id = dbi_result_get_ulonglong(res, "id");
    obj->jobunit_id = dbi_result_get_ulonglong(res, "jobunit_id");
    obj->proc_jobunit_id =
        dbi_result_get_ulonglong(res, "proc_jobunit_id");
    obj->hist_jobunit_id =
        dbi_result_get_ulonglong(res, "hist_jobunit_id");

    obj->host = jhkdb_get_string(res, "host");
    obj->port = dbi_result_get_int(res, "port");
    obj->username = jhkdb_get_string(res, "username");
    obj->password = jhkdb_get_string(res, "password");
    obj->scheme = jhkdb_get_string(res, "scheme");
    obj->path = jhkdb_get_string(res, "path");
    obj->auth = jhkdb_get_string(res, "auth");
    obj->codepage = dbi_result_get_int(res, "codepage");
    obj->command = jhkdb_get_string(res, "command");

    return 0;
}
//*******************************************************************
// UPDATE SERVER COLOR
//*******************************************************************
static void update_server_color(trx *t, struct probe_result *prv)
{
  dbi_result result;
  int maxcolor, newcolor;

  if (t->def->server < 2) return;

  if (t->probe->fuse && (t->res->color < prv->color)) {
    // if this probe acts like a fuse, use the previous color if it's higher
    newcolor = prv->color;
  } else {
    newcolor = t->res->color;
  }
  maxcolor = newcolor; // init

  result = db_query(t->probe->db, 0,
                    "select max(color) as color from pr_status where server = '%u'", t->def->server);
  if (result) {
    if (dbi_result_next_row(result)) {
      maxcolor = dbi_result_get_int(result, "color");
    }
  }
  dbi_result_free(result);
  //if (t->res->color <= maxcolor) return;

  result = db_query(t->probe->db, 0,
                    // update server set color = '200' where id = '345'
                    "update %s set %s = '%u' where %s = '%u'",
                     OPT_ARG(SERVER_TABLE_NAME), OPT_ARG(SERVER_TABLE_COLOR_FIELD), 
                     maxcolor, OPT_ARG(SERVER_TABLE_ID_FIELD), t->def->server);
  dbi_result_free(result);
}
Exemple #5
0
/////////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// Purpose:
//
// Parameters:
//
// Return value:
//
// Author: Komatsu Yuji(Zheng Chuyu)
//
/////////////////////////////////////////////////////////////////////////////////
int datejob_load(datejob_t * obj, dbi_result res)
{
    jhklog_trace("In %s()", __func__);
    if (obj == NULL || res == NULL)
        return -1;

    datejob_init(obj);

    obj->id = dbi_result_get_ulonglong(res, "id");
    obj->jobunit_id = dbi_result_get_ulonglong(res, "jobunit_id");
    obj->proc_jobunit_id =
        dbi_result_get_ulonglong(res, "proc_jobunit_id");
    obj->hist_jobunit_id =
        dbi_result_get_ulonglong(res, "hist_jobunit_id");

    obj->baseday = dbi_result_get_int(res, "baseday");
    obj->beginning_of_week = dbi_result_get_int(res, "beginning_of_week");

    return 0;
}
Exemple #6
0
/////////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// Purpose:
//
// Parameters:
//
// Return value:
//
// Author: Komatsu Yuji(Zheng Chuyu)
//
/////////////////////////////////////////////////////////////////////////////////
int datecond_load(datecond_t * obj, dbi_result res)
{
    jhklog_trace("In %s()", __func__);
    if (obj == NULL || res == NULL)
        return -1;

    datecond_init(obj);

    obj->id = dbi_result_get_ulonglong(res, "id");
    obj->jobunit_id = dbi_result_get_ulonglong(res, "jobunit_id");
    obj->proc_jobunit_id =
        dbi_result_get_ulonglong(res, "proc_jobunit_id");
    obj->hist_jobunit_id =
        dbi_result_get_ulonglong(res, "hist_jobunit_id");

    obj->kind = dbi_result_get_int(res, "kind");
    obj->cond = dbi_result_get_int(res, "cond");
    obj->calendar_id = dbi_result_get_ulonglong(res, "calendar_id");
    obj->negative = dbi_result_get_int(res, "negative");

    return 0;
}
//*******************************************************************
// SEE IF THE USER HAS BEEN NOTIFIED FOR THIS COLOR
//*******************************************************************
void get_previous_pr_hist(trx *t)
{
  dbi_result result;

  t->res->changed = 0;
  strcpy(t->res->notified, "yes"); // if we cannot find pr_hist record, assume the user has already been warned
  result = db_query(t->probe->db, 0,
                    "select   id, stattime, notified, prv_color "
                    "from     pr_hist  "
                    "where    probe = '%u' and class = '%u' and stattime <= '%u' "
                    "order by stattime desc limit 1",
                    t->def->probeid, t->probe->class, t->res->stattime);
  if (!result) return;
  if (dbi_result_next_row(result)) {
    t->res->prevhistid = dbi_result_get_longlong(result, "id");
    t->res->changed = dbi_result_get_int(result, "stattime");
    strcpy(t->res->notified, dbi_result_get_string(result, "notified"));
    t->res->prevhistcolor = dbi_result_get_int(result, "prv_color");
  }
  dbi_result_free(result);
  return;
}
//*******************************************************************
// RETRIEVE FOLLOWING RAW RECORD FROM DATABASE (well just the color)
//*******************************************************************
static struct probe_result *get_following_record(trx *t)
{
  dbi_result result;
  struct probe_result *nxt = NULL;

  if (!t->probe->store_results) return(NULL); // we probably don't have a xxxx_raw table

  result = db_query(t->probe->db, 0,
                    "select   color, stattime "
                    "from     pr_%s_raw use index(probstat) "
                    "where    probe = '%u' and stattime > '%u' "
                    "order by stattime asc limit 1", 
                    t->res->name, t->def->probeid, t->res->stattime);
  if (result) {
    if (dbi_result_next_row(result)) {
      nxt = g_malloc0(sizeof(struct probe_result));

      nxt->color = dbi_result_get_int(result, "color");
      nxt->stattime = dbi_result_get_int(result, "stattime");
    }
    dbi_result_free(result);
  }
  return(nxt);
}
Exemple #9
0
/////////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// Purpose:
//
// Parameters:
//
// Return value:
//
// Author: Komatsu Yuji(Zheng Chuyu)
//
/////////////////////////////////////////////////////////////////////////////////
int calendar_load(calendar_t * obj, dbi_result res)
{
    jbxlog_trace("In %s()", __func__);

    if (obj == NULL || res == NULL)
        return -1;

    calendar_init(obj);
    obj->id = dbi_result_get_ulonglong(res, "id");
    obj->name = jbxdb_get_string(res, "name");
    obj->description = jbxdb_get_string(res, "description");
    obj->kind = dbi_result_get_int(res, "kind");
    obj->created_at = jbxdb_get_datetime(res, "created_at");
    obj->updated_at = jbxdb_get_datetime(res, "updated_at");

    return 0;
}
//*******************************************************************
// IS THIS SLOT COMPLETELY FILLED?
//*******************************************************************
int slot_is_complete(trx *t, int i, guint slotlow, guint slothigh)
{
  dbi_result result;
  int val = FALSE;

  /* 
   * don't know how many raw records go into a day slot, 
   * so we fake it is filled, so it'll always be summarized
   */
  if (i == 0) return 1;
  result = db_query(t->probe->db, 0,
                    "select count(*) as count from pr_%s_%s use index(probstat) "
                    "where  probe = '%u' and stattime >= '%u' and stattime <= '%u'",
                    t->res->name, summ_info[i].from, t->def->probeid, slotlow, slothigh);
  if (!result) return(FALSE);
  if (dbi_result_next_row(result)) {
    if (dbi_result_get_int(result, "count") >= summ_info[i].perslot) {
      val = TRUE;
    }
  }
  dbi_result_free(result);
  return(val);
}
Exemple #11
0
int dbi_result_get_long(dbi_result Result, const char *fieldname) {
  return dbi_result_get_int(Result, fieldname);
}
//*******************************************************************
// retrieve the definitions + status
// get it from the cache. if there but too old: delete
// If a probe definition does not exist, it will be created.
// in case of mysql-has-gone-away type errors, we keep on running, 
// it will be caught later-on.
//*******************************************************************
void *bb_cpu_get_def(trx *t, int create)
{
  struct probe_def *def;
  struct bb_cpu_result *res = (struct bb_cpu_result *)t->res;
  dbi_result result;
  time_t now = time(NULL);

  if (res->color != STAT_PURPLE && res->server == 0) { 
    // first we find the serverid, this will be used to find the probe definition in the hashtable
    result = db_query(t->probe->db, 0, query_server_by_name, res->hostname, res->hostname, 
                      res->hostname, res->hostname, res->hostname);
    if (!result) {
      return(NULL);
    }
    if (dbi_result_next_row(result)) {
      res->server   = dbi_result_get_int_idx(result, 0);
    } else {
      LOG(LOG_NOTICE, "%s:%u@%s: server %s not found", res->realm, res->stattime, t->fromhost, res->hostname);
      dbi_result_free(result);
      return(NULL);
    }
    dbi_result_free(result);
  }

  // look in the cache for the def
  def = g_hash_table_lookup(t->probe->cache, &res->server);
  if (def && def->stamp < now - (120 + uw_rand(240))) { // older then 2 - 6 minutes?
     g_hash_table_remove(t->probe->cache, &res->server);
     def = NULL;
  }
  // if not there retrieve from database and insert in hash
  if (def == NULL) {
    def = g_malloc0(t->probe->def_size);
    def->stamp    = time(NULL);
    def->server   = res->server;
    def->pgroup   = 1;
    strcpy(def->hide, "no");

    // first find the definition based on the serverid
    result = db_query(t->probe->db, 0, "select id, yellow, red, contact, hide, email, sms, delay "
                                    "from pr_%s_def where server = '%u'", 
                      res->name, res->server);
    if (!result) {
      g_free(def);
      return(NULL);
    }
    if (dbi_result_get_numrows(result) == 0) { // DEF RECORD NOT FOUND
      char sequence[40];
      // no def record found? Create one. 
      dbi_result_free(result);
      result = db_query(t->probe->db, 0, 
                        "insert into pr_%s_def (server, description) values ('%u', '%s')", 
                         res->name, res->server, res->hostname);
      dbi_result_free(result);
      sprintf(sequence, "pr_%s_def_id_seq", res->name);
      def->probeid = dbi_conn_sequence_last(t->probe->db, sequence);
      LOG(LOG_NOTICE, "%s:%u@%s: pr_%s_def created for %s, id = %u", 
          res->realm, res->stattime, t->fromhost, res->name, res->hostname, def->probeid);
      result = db_query(t->probe->db, 0, "select id, yellow, red, contact, hide, email, sms, delay "
                                      "from pr_%s_def where id = '%u'", 
                        res->name, def->probeid);
    }
    if (!dbi_result_next_row(result)) {
      LOG(LOG_NOTICE, "%s:%u@%s: no pr_%s_def found for server %u - skipped", 
          res->realm, res->stattime, t->fromhost, res->name, res->server);
      dbi_result_free(result);
      g_free(def);
      return(NULL);
    }

    def->probeid = dbi_result_get_int(result, "id");
    def->yellow  = dbi_result_get_float(result, "yellow");
    def->red     = dbi_result_get_float(result, "red");
    def->contact = dbi_result_get_int(result, "contact");
    strcpy(def->hide, dbi_result_get_string_default(result, "hide", "no"));
    strcpy(def->email, dbi_result_get_string_default(result, "email", ""));
    strcpy(def->sms, dbi_result_get_string_default(result, "sms", ""));
    def->delay   = dbi_result_get_int(result, "delay");

    dbi_result_free(result);
    result = db_query(t->probe->db, 0, 
                      "select color "
                      "from   pr_status "
                      "where  class = '%u' and probe = '%u'", t->probe->class, def->probeid);
    if (result) {
      if (dbi_result_next_row(result)) {
        def->color   = dbi_result_get_int(result, "color");
      } else {
        LOG(LOG_NOTICE, "%s:%u@%s: pr_status record for %s id %u not found", 
           res->realm, res->stattime, t->fromhost, res->name, def->probeid);
      }
      dbi_result_free(result);
    }
    if (!def->color) def->color = res->color;

    result = db_query(t->probe->db, 0, 
                      "select stattime from pr_%s_raw use index(probstat) "
                      "where probe = '%u' order by stattime desc limit 1",
                       res->name, def->probeid);
    if (result) {
      if (dbi_result_next_row(result)) {
        def->newest = dbi_result_get_int(result, "stattime");
      }
      dbi_result_free(result);
    }
    g_hash_table_insert(t->probe->cache, guintdup(def->server), def);
  }
Exemple #13
0
void refresh_database(database *db)
{
  dbi_result result;
  char qry[1024];
  char *error;

  sprintf(qry,  "SELECT pr_mysqlstats_def.id, pr_mysqlstats_def.domid, pr_mysqlstats_def.tblid, pr_realm.name, "
                "       pr_mysqlstats_def.ipaddress, pr_mysqlstats_def.dbname, "
                "       pr_mysqlstats_def.dbuser, pr_mysqlstats_def.dbpasswd,"
                "       pr_mysqlstats_def.yellow,  pr_mysqlstats_def.red "
                "FROM   pr_mysqlstats_def, pr_realm "
                "WHERE  pr_mysqlstats_def.id > 1 and pr_mysqlstats_def.disable <> 'yes'"
                "       and pr_mysqlstats_def.pgroup = '%d' and pr_realm.id = pr_mysqlstats_def.domid",
                (unsigned)OPT_VALUE_GROUPID);

  result = db_query(db, 1, qry);
  if (!result) {
    return;
  }
    
  while (dbi_result_next_row(result)) {
    int id;
    struct probedef *probe;
    
    id = dbi_result_get_int(result, "id");
    probe = g_hash_table_lookup(cache, &id);
    if (!probe) {
      probe = g_malloc0(sizeof(struct probedef));
      probe->id = id;
      if (dbi_result_get_int(result, "domid") > 1) {
        probe->probeid = dbi_result_get_int(result, "tblid");
        probe->realm = dbi_result_get_string_copy(result, "name");
      } else {
        probe->probeid = probe->id;
      }
      //fprintf(stderr, "Adding %u\n", id);
      g_hash_table_insert(cache, guintdup(id), probe);
    }

    if (probe->ipaddress) g_free(probe->ipaddress);
    probe->ipaddress = dbi_result_get_string_copy(result, "ipaddress");
    if (probe->dbname) g_free(probe->dbname);
    probe->dbname = dbi_result_get_string_copy(result, "dbname");
    if (probe->dbuser) g_free(probe->dbuser);
    probe->dbuser = dbi_result_get_string_copy(result, "dbuser");
    if (probe->dbpasswd) g_free(probe->dbpasswd);
    probe->dbpasswd = dbi_result_get_string_copy(result, "dbpasswd");
    probe->yellow = dbi_result_get_float(result, "yellow");
    probe->red = dbi_result_get_float(result, "yellow");
    if (probe->msg) g_free(probe->msg);
    probe->msg = NULL;
    probe->seen = 1;
  }
  dbi_result_free(result);
  if (dbi_conn_error(db, &error) == DBI_ERROR_NONE) {
    g_hash_table_foreach_remove(cache, return_seen, NULL);
  } else {
    LOG(LOG_ERR, "%s", error);
    g_hash_table_foreach(cache, reset_seen, NULL);
  }
}
//*******************************************************************
// SUMMARIZE A TABLE INTO AN OLDER PERIOD
//*******************************************************************
static void mysqlstats_summarize(trx *t, char *from, char *into, guint slot, guint slotlow, guint slothigh, gint resummarize)
{
  dbi_result result;
  struct probe_def *def = (struct probe_def *)t->def;
  float avg_yellow, avg_red;
  int avg_selectq;
  int avg_insertq;
  int avg_updateq;
  int avg_deleteq;
  guint stattime;
  guint max_color;

  stattime = slotlow + ((slothigh-slotlow)/2);

  result = db_query(t->probe->db, 0,
                    "select avg(selectq) as avg_selectq, avg(insertq) as avg_insertq, avg(updateq) as avg_updateq, "
                    "       avg(deleteq) as avg_deleteq, max(color) as max_color, avg(yellow) as avg_yellow, avg(red) as avg_red"
                    "from   pr_mysqlstats_%s use index(probstat) "
                    "where  probe = '%d' and stattime >= %d and stattime < %d",
                    from, def->probeid, slotlow, slothigh);

  if (!result) return;
  if (dbi_result_get_numrows(result) == 0) { // no records found
    LOG(LOG_NOTICE, "nothing to summarize from %s for probe %u %u %u",
                       from, def->probeid, slotlow, slothigh);
    dbi_result_free(result);
    return;
  }

  if (!dbi_result_next_row(result)) {
    dbi_result_free(result);
    return;
  }
  if (dbi_result_field_is_null_idx(result, 0)) {
    LOG(LOG_NOTICE, "nothing to summarize from %s for probe %u %u %u", 
                       from, def->probeid, slotlow, slothigh);
    dbi_result_free(result);
    return;
  }

  avg_selectq = dbi_result_get_int(result, "avg_selectq");
  avg_insertq = dbi_result_get_int(result, "avg_insertq");
  avg_updateq = dbi_result_get_int(result, "avg_updateq");
  avg_deleteq = dbi_result_get_int(result, "avg_deleteq");
  max_color   = dbi_result_get_int(result, "max_color");
  avg_yellow  = dbi_result_get_float(result, "avg_yellow");
  avg_red     = dbi_result_get_float(result, "avg_red");
  dbi_result_free(result);

  if (resummarize) {
    // delete old values
    result = db_query(t->probe->db, 0,
                    "delete from pr_mysqlstats_%s where probe = '%u' and stattime = '%u'",
                    into, def->probeid, stattime);
    dbi_result_free(result);
  }

  result = db_query(t->probe->db, 0,
                    "insert into pr_mysqlstats_%s (selectq, insertq, updateq, deleteq, probe, color, stattime, "
                    "                              yellow, red, slot) "
                    "            values ('%u', '%u', '%u', '%u', '%d', '%u', '%d', "
                    "                    '%f', '%f', '%u')",
                    into,  avg_selectq, avg_insertq, avg_updateq, avg_deleteq, def->probeid, 
                    max_color, stattime, avg_yellow, avg_red, slot);

  dbi_result_free(result);
}
Exemple #15
0
//*******************************************************************
// SUMMARIZE A TABLE INTO AN OLDER PERIOD
//*******************************************************************
static void bb_cpu_summarize(trx *t, char *from, char *into, guint slot, guint slotlow, guint slothigh, gint resummarize)
{
  dbi_result result;
  struct probe_def *def = (struct probe_def *)t->def;
  float avg_yellow, avg_red;
  gfloat avg_loadavg;
  guint avg_user, avg_system, avg_idle;
  guint avg_swapped, avg_free, avg_buffered, avg_cached, avg_used;
  guint stattime;
  guint max_color;

  stattime = slotlow + ((slothigh-slotlow)/2);

  result = db_query(t->probe->db, 0,
                    "select avg(loadavg) as avg_loadavg, avg(user) as avg_user, avg(system) as avg_system, "
                    "       avg(idle) as avg_idle, avg(swapped) as avg_swapped, avg(free) as avg_free, "
                    "       avg(buffered) as avg_buffered, avg(cached) as avg_cached, avg(used) as avg_used, "
                    "       max(color) as max_color, avg(yellow) as avg_yellow, avg(red) as avg_red " 
                    "from   pr_bb_cpu_%s use index(probstat) "
                    "where  probe = '%d' and stattime >= %d and stattime < %d",
                    from, def->probeid, slotlow, slothigh);
  
  if (!result) return;
  if (dbi_result_get_numrows(result) == 0) { // no records found
    LOG(LOG_NOTICE, "nothing to summarize from %s for probe %u %u %u",
                       from, def->probeid, slotlow, slothigh);
    dbi_result_free(result);
    return;
  }

  if (!dbi_result_next_row(result)) {
    dbi_result_free(result);
    return;
  }
  if (dbi_result_field_is_null_idx(result, 0)) {
    LOG(LOG_NOTICE, "nothing to summarize from %s for probe %u %u %u", 
                       from, def->probeid, slotlow, slothigh);
    dbi_result_free(result);
    return;
  }

  avg_loadavg = dbi_result_get_float(result, "avg_loadavg");
  avg_user    = dbi_result_get_int(result, "avg_user");
  avg_system  = dbi_result_get_int(result, "avg_system");
  avg_idle    = dbi_result_get_int(result, "avg_idle");
  avg_swapped = dbi_result_get_int(result, "avg_swapped");
  avg_free    = dbi_result_get_int(result, "avg_free");
  avg_buffered= dbi_result_get_int(result, "avg_buffered");
  avg_cached  = dbi_result_get_int(result, "avg_cached");
  avg_used    = dbi_result_get_int(result, "avg_used");
  max_color   = dbi_result_get_int(result, "max_color");
  avg_yellow  = dbi_result_get_float(result, "avg_yellow");
  avg_red     = dbi_result_get_float(result, "avg_red");
  dbi_result_free(result);

  if (resummarize) {
    // delete old values
    result = db_query(t->probe->db, 0,
                    "delete from pr_bb_cpu_%s where probe = '%u' and stattime = '%u'",
                    into, def->probeid, stattime);
    dbi_result_free(result);
  }
  result = db_query(t->probe->db, 0,
                    "insert into pr_bb_cpu_%s (loadavg, user, system, idle, swapped, free, buffered, cached, " 
                    "                          used, probe, color, stattime, yellow, red, slot) "
                    "            values ('%f', '%u', '%u', '%u', '%u', '%u', '%u', '%u', "
                    "                    '%u', '%d', '%u', '%d', '%f', '%f', '%u')",
                    into, 
                    avg_loadavg, avg_user, avg_system, avg_idle, 
                    avg_swapped, avg_free, avg_buffered, avg_cached, 
                    avg_used, def->probeid, max_color, stattime,
                    avg_yellow, avg_red, slot);
  dbi_result_free(result);
}
Exemple #16
0
unsigned int dbi_result_get_uint(dbi_result Result, const char *fieldname) {
  return (unsigned int)dbi_result_get_int(Result, fieldname);
}
Exemple #17
0
unsigned int dbi_result_get_fields(dbi_result Result, const char *format, ...) {
  char **tokens, **fieldnames;
  unsigned int curidx = 0, numtokens = 0, uflag;
  va_list ap;

  if (!RESULT) return DBI_FIELD_ERROR;

  numtokens = _parse_field_formatstr(format, &tokens, &fieldnames);

  if (numtokens == DBI_FIELD_ERROR) {
    return numtokens;
  }
	
  va_start(ap, format);
  while (curidx < numtokens) {
    uflag = strlen(tokens[curidx]) > 1 && tokens[curidx][0] == 'u';
    switch (tokens[curidx][strlen(tokens[curidx])-1]) {
    case 'c': /* char */
      if (uflag) /* unsigned */
        *va_arg(ap, unsigned char *) = dbi_result_get_uchar(Result, fieldnames[curidx]);
      else
        *va_arg(ap, char *) = dbi_result_get_char(Result, fieldnames[curidx]);
      break;
    case 'h': /* sHort ("S"tring was taken) */
      if (uflag) /* unsigned */
        *va_arg(ap, unsigned short *) = dbi_result_get_ushort(Result, fieldnames[curidx]);
      else
        *va_arg(ap, short *) = dbi_result_get_short(Result, fieldnames[curidx]);
      break;
    case 'l': /* 4-byte integer (both l and i work) */
    case 'i':
      if (uflag) /* unsigned */
        *va_arg(ap, unsigned int *) = dbi_result_get_uint(Result, fieldnames[curidx]);
      else
        *va_arg(ap, int *) = dbi_result_get_int(Result, fieldnames[curidx]);
      break;
    case 'L': /* long long */
      if (uflag) /* unsigned */
        *va_arg(ap, unsigned long long *) = dbi_result_get_ulonglong(Result, fieldnames[curidx]);
      else
        *va_arg(ap, long long *) = dbi_result_get_longlong(Result, fieldnames[curidx]);
      break;
    case 'f': /* float */
      *va_arg(ap, float *) = dbi_result_get_float(Result, fieldnames[curidx]); 
      break;
    case 'd': /* double */
      *va_arg(ap, double *) = dbi_result_get_double(Result, fieldnames[curidx]); 
      break;
    case 's': /* string */
      *va_arg(ap, const char **) = dbi_result_get_string(Result, fieldnames[curidx]); 
      break;
    case 'b': /* binary */
      *va_arg(ap, const unsigned char **) = dbi_result_get_binary(Result, fieldnames[curidx]); 
      break;
    case 'S': /* string copy */
      *va_arg(ap, char **) = dbi_result_get_string_copy(Result, fieldnames[curidx]); 
      break;
    case 'B': /* binary copy */
      *va_arg(ap, unsigned char **) = dbi_result_get_binary_copy(Result, fieldnames[curidx]); 
      break;
    case 'm': /* datetiMe (what... you have any better ideas?? */
      *va_arg(ap, time_t *) = dbi_result_get_datetime(Result, fieldnames[curidx]);
      break;
    }
    curidx++;
  }
  va_end(ap);

  _free_string_list(tokens, numtokens);
  _free_string_list(fieldnames, numtokens);
  return numtokens;
}
Exemple #18
0
static void _bind_helper_int(_field_binding_t *binding) {
  *(int *)binding->bindto = dbi_result_get_int((dbi_result)binding->result, binding->fieldname);
}
int rpserver_db_pair_exec(dbi_conn *db, const char *localid, const char *name, char *buf, size_t maxlen)
{
    dbi_result *result; 
    int len = 0;
    int boxid = -1;

    result = dbi_conn_queryf(db,  "DELETE FROM "DB_TABLE_DEVICES" WHERE name='%s' AND iid IS NULL\n", name);

    if(result){
	dbi_result_free(result);
    } else {
	const char *err;
        dbi_conn_error(db, &err);
        LOG(LOG_INFO, "%s():%d - dbi error querying database (%s)\n", __FUNCTION__, __LINE__, err);
        return -1;
    }

    result = dbi_conn_queryf(db,  "SELECT device_id FROM "DB_TABLE_DEVICES" WHERE localid='%s' AND name IS NULL\n", localid);
    
    if(result){
	if(dbi_result_next_row(result)) {
	    boxid =  dbi_result_get_int(result, "device_id");
	    LOG(LOG_INFO, "%s():%d - boxid is %d\n", __FUNCTION__, __LINE__, boxid);
	}
	dbi_result_free(result);
    } else {
	const char *err;
        dbi_conn_error(db, &err);
        LOG(LOG_INFO, "%s():%d - dbi error querying database (%s)\n", __FUNCTION__, __LINE__, err);
        return -1;
    }
    
    if(boxid < 0){
	LOG(LOG_INFO, "%s():%d - boxid %d !!!!\n", __FUNCTION__, __LINE__, boxid);
	return -1;
    }
    
    result = dbi_conn_queryf(db, "UPDATE "DB_TABLE_DEVICES" SET name='%s' WHERE device_id=%d", name, boxid);
    

    if(result){
	dbi_result_free(result);
    } else {
	const char *err;
        dbi_conn_error(db, &err);
        LOG(LOG_INFO, "%s():%d - dbi error querying database (%s)\n", __FUNCTION__, __LINE__, err);
        return -1;
    } 

    LOG(LOG_INFO, "%s():%d - name updated to '%s' for moxid  %d !!!!\n", __FUNCTION__, __LINE__, name, boxid);

    
    result = dbi_conn_queryf(db, "SELECT hname, localid FROM "DB_TABLE_DEVICES" WHERE  device_id=%d", boxid);
    
    if(result){
	if(dbi_result_next_row(result)) {
	    len +=  snprintf(buf+len, maxlen-len, "%s: %s",
			     dbi_result_get_string(result, "localid"),
			     dbi_result_get_string(result, "hname")
			     );
	}
	dbi_result_free(result);
    } else {
        const char *err;
        dbi_conn_error(db, &err);
        LOG(LOG_INFO, "%s():%d - dbi error querying database (%s)\n", 
            __FUNCTION__, __LINE__, err);
        return -1;
    }

    LOG(LOG_INFO, "%s():%d - read localiid and hname:  %s !!!!\n", __FUNCTION__, __LINE__, name, buf);
    
    int other_pwid = -1;
    int working_pwid = 0;

    result = dbi_conn_queryf(db,  
			     "SELECT pwid , used, atid  FROM acc_devices_pwl JOIN acc_types USING(atid)"
			     " WHERE agid = 1 AND acc_devices_pwl.enable = 1 AND acc_types.enable =1"
			     " AND id = %d  ORDER BY atid ASC", boxid);

    if(result){
	if(dbi_result_next_row(result)) {
	    other_pwid = dbi_result_get_int(result, "pwid");
	    if(dbi_result_get_int(result, "used")==1)
		working_pwid++;
	    if(dbi_result_get_int(result, "atid")== REMACC_TYPE_MAC)
		working_pwid++;
	}
	dbi_result_free(result);
    } else {
        const char *err;
        dbi_conn_error(db, &err);
        LOG(LOG_INFO, "%s():%d - dbi error querying database (%s)\n", 
            __FUNCTION__, __LINE__, err);
        return -1;
    }

    LOG(LOG_INFO, "%s():%d - searched for other pwid :  %d !!!!\n", __FUNCTION__, __LINE__, working_pwid);

    if(working_pwid>0){
	LOG(LOG_INFO, "%s():%d - has working pwid, no new pwid are issued (%d)\n" ,__FUNCTION__, __LINE__,working_pwid);
	return len;
    }	

    char other_pwid_str[32];
    if(other_pwid < 0){
	sprintf(other_pwid_str, "NULL");
    } else {
	sprintf(other_pwid_str, "%d", other_pwid);
    }

    result = dbi_conn_queryf(db,  "INSERT INTO  `rembox`.`acc_devices_pwl`"
			     "        (`pwid` ,`atid` ,`id` ,`username` ,`password` ,`enable` ,`exported` ,`used` ,`created` ,`replaced`)"
			     " VALUES (NULL ,  %d,  %d,  '%s', NULL ,  '1',  '0',  '0', CURRENT_TIMESTAMP ,  %s)"
			     , REMACC_TYPE_MAC, boxid, name, other_pwid_str);

    if(result){
	dbi_result_free(result);
    } else {
	const char *err;
        dbi_conn_error(db, &err);
        LOG(LOG_INFO, "%s():%d - dbi error querying database (%s)\n", __FUNCTION__, __LINE__, err);
        return -1;
    } 

    LOG(LOG_INFO, "%s():%d - inserted new pwid :  %d !!!!\n", __FUNCTION__, __LINE__, dbi_conn_sequence_last(db, NULL));


    return len;

}
Exemple #20
0
//*******************************************************************
// retrieve the definitions + status
// get it from the cache. if there but too old: delete
// If a probe definition does not exist, it will be created.
// in case of mysql-has-gone-away type errors, we keep on running, 
// it will be caught later-on.
//*******************************************************************
void *bb_get_def(trx *t, int create)
{
  struct probe_def *def;
  struct bb_result *res = (struct bb_result *)t->res;
  dbi_result result;

  def = g_malloc0(t->probe->def_size);
  def->stamp    = time(NULL);
  def->server   = res->server;
  def->pgroup   = 1;
  strcpy(def->hide, "no");

  if (res->color == STAT_PURPLE && res->probeid) {
    // find the definition based on the probe id
    result = db_query(t->probe->db, 0,
                      "select id, contact, hide, email, sms, delay from pr_bb_def "
                      "where  id = '%u'", res->probeid);
    if (!result) {
      g_free(def);
      return(NULL);
    }
  } else {
    if (res->server == 0) {
      // first we find the serverid, this will be used to find the probe definition in the database
      if (!query_server_by_name) {
        LOG(LOG_WARNING, "%s:%u@%s: don't know how to find %s by name", 
            res->realm, res->stattime, t->fromhost, res->hostname);
        g_free(def);
        return(NULL);
      }
      result = db_query(t->probe->db, 0, query_server_by_name, res->hostname, res->hostname, 
                        res->hostname, res->hostname, res->hostname);
      if (!result) {
        g_free(def);
        return(NULL);
      }
      if (dbi_result_next_row(result)) {
        res->server   = dbi_result_get_int_idx(result, 0);
      } else {
        LOG(LOG_WARNING, "%s:%u@%s: server %s not found", res->realm, res->stattime, t->fromhost, res->hostname);
        dbi_result_free(result);
        g_free(def);
        return(NULL);
      }
      dbi_result_free(result);
    }

    // first find the definition based on the serverid
    result = db_query(t->probe->db, 0,
                      "select id, contact, hide, email, sms, delay from pr_bb_def "
                      "where  bbname = '%s' and server = '%u'", res->bbname, res->server);
    if (!result) {
      g_free(def);
      return(NULL);
    }
  }
  if (dbi_result_get_numrows(result) == 0) { // DEF RECORD NOT FOUND
    char sequence[40];
    dbi_result_free(result);
    result = db_query(t->probe->db, 0,
                      "insert into pr_%s_def (server, ipaddress, description, bbname) "
                      "            values ('%u', '%s', '%s', '%s')", 
                      res->name, res->server, res->ipaddress ? res->ipaddress : "", 
                      res->hostname, res->bbname);
    dbi_result_free(result);
    sprintf(sequence, "pr_%s_def_id_seq", res->name);
    def->probeid  = dbi_conn_sequence_last(t->probe->db, sequence);
    LOG(LOG_NOTICE, "%s:%u@%s: pr_bb_def %s created for %s, id = %u", 
        res->realm, res->stattime, t->fromhost, res->bbname, res->hostname, def->probeid);
    result = db_query(t->probe->db, 0,
                    "select id, contact, hide, email, sms, delay from pr_bb_def "
                    "where  bbname = '%s' and server = '%u'", res->bbname, res->server);
    if (!result) return(NULL);
  }
  if (!dbi_result_next_row(result)) {
    LOG(LOG_NOTICE, "%s:%u@%s: no pr_%s_def found for server %u - skipped", 
         res->realm, res->stattime, t->fromhost, res->name, res->server);
    dbi_result_free(result);
    return(NULL);
  }
  def->probeid = dbi_result_get_int(result, "id");
  def->contact = dbi_result_get_int(result, "contact");
  strcpy(def->hide, dbi_result_get_string_default(result, "hide", "no"));
  strcpy(def->email, dbi_result_get_string_default(result, "email", ""));
  strcpy(def->sms, dbi_result_get_string_default(result, "sms", ""));
  def->delay = dbi_result_get_int(result, "delay");

  dbi_result_free(result);

  // definition found, get the pr_status
  result = db_query(t->probe->db, 0,
                    "select color, stattime "
                    "from   pr_status "
                    "where  class = '%u' and probe = '%u'", t->probe->class, def->probeid);
  if (result) {
    if (dbi_result_next_row(result)) {
      def->color   = dbi_result_get_int(result, "color");
      def->newest  = dbi_result_get_int(result, "stattime");
    } else {
      LOG(LOG_NOTICE, "%s:%u@%s: pr_status record for %s id %u (server %s) not found", 
                       res->realm, res->stattime, t->fromhost, res->name, def->probeid, res->hostname);
    }
    dbi_result_free(result);
  }
  if (!def->color) def->color = res->color;
  res->probeid = def->probeid;

  return(def);
}