Exemplo n.º 1
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
static int process_jatrap(zbx_sock_t * sock, char *s)
{
    int ret = SUCCEED;
    struct zbx_json_parse jp;
    char value[MAX_STRING_LEN];
    char *message = NULL;
    static JOBARG_JOBNET_INFO ji;
    zbx_rtrim(s, " \r\n");
    zabbix_log(LOG_LEVEL_DEBUG, "Jatrapper got [%s] len %zd", s,
               strlen(s));

    /*JSON Format check */
    if (SUCCEED != zbx_json_open(s, &jp)) {
        ja_log("JATRAPPER200037", 0, NULL, 0);
        message =
            zbx_dsprintf(message,
                         "Received message error: [JSON format error]");
        ret = FAIL;
        reply_jobresult_response(sock, ret, message);
        zbx_free(message);
        return ret;
    }

    /*[kind] check */
    if (SUCCEED !=
        zbx_json_value_by_name(&jp, JA_PROTO_TAG_KIND, value,
                               sizeof(value))) {
        ja_log("JATRAPPER200036", 0, NULL, 0);
        message =
            zbx_dsprintf(message,
                         "Received message error: [kind] not found");
        ret = FAIL;
        reply_jobresult_response(sock, ret, message);
        zbx_free(message);
        return ret;
    }
    if (0 == strcmp(value, JA_PROTO_VALUE_JOBRESULT)) {
        /*from Job agent */
    } else if (0 == strcmp(value, JA_PROTO_VALUE_JOBNETRUN)) {
        /*from external I/F */
        message = evaluate_jobnetrun(sock, &jp, &ret);
        reply_jobnetrun_response(sock, ret, message);
    } else if (0 == strcmp(value, JA_PROTO_VALUE_JOBNETSTATUSRQ)) {
        /*from jobnet-status request */
        init_jobnetinfo(&ji);
        message = evaluate_jobnetstatusrq(sock, &jp, &ret, &ji);
        reply_jobnetstatusrq_response(sock, ret, &ji, message);
    } else {
        ja_log("JATRAPPER200035", 0, NULL, 0);
        message =
            zbx_dsprintf(message,
                         "Received message error: [kind] is not correct.");
        ret = FAIL;
        reply_jobresult_response(sock, ret, message);
    }
    zbx_free(message);
    return ret;
}
Exemplo n.º 2
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int jarun_icon_info_get_status(const zbx_uint64_t inner_jobnet_id,
                               char *get_job_id, const zbx_uint64_t inner_job_id)
{
    char *tp;
    DB_RESULT result;
    DB_ROW row;
    int status;
    zbx_uint64_t sub_inner_job_id, sub_inner_jobnet_id;
    const char *__function_name = "jarun_icon_info_get_status";

    zabbix_log(LOG_LEVEL_DEBUG,
               "In %s() inner_jobnet_id: " ZBX_FS_UI64 " get_job_id: %s",
               __function_name, inner_jobnet_id, get_job_id);

    status = -1;
    sub_inner_job_id = 0;
    sub_inner_jobnet_id = inner_jobnet_id;
    tp = strtok(get_job_id, "/");
    while (tp != NULL) {
        if (sub_inner_job_id > 0) {
            result =
                DBselect
                ("select link_inner_jobnet_id from ja_run_icon_jobnet_table"
                 " where inner_job_id = " ZBX_FS_UI64, sub_inner_job_id);
            if (NULL != (row = DBfetch(result))) {
                ZBX_STR2UINT64(sub_inner_jobnet_id, row[0]);
            } else {
                ja_log("JARUNICONINFO200001", 0, NULL, inner_job_id,
                       __function_name, sub_inner_job_id);
                status = -1;
                DBfree_result(result);
                break;
            }
            DBfree_result(result);
        }

        result =
            DBselect
            ("select inner_job_id, status from ja_run_job_table"
             " where inner_jobnet_id = " ZBX_FS_UI64
             " and job_id = '%s'", sub_inner_jobnet_id, tp);
        if (NULL != (row = DBfetch(result))) {
            ZBX_STR2UINT64(sub_inner_job_id, row[0]);
            status = atoi(row[1]);
        } else {
            ja_log("JARUNICONINFO200002", 0, NULL, inner_job_id,
                   __function_name, tp, inner_job_id);
            status = -1;
            DBfree_result(result);
            break;
        }
        DBfree_result(result);
        tp = strtok(NULL, "/");
    }
    return status;
}
Exemplo n.º 3
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int job_exec_auth(JOBARG_EXEC_REQUEST er)
{
    int ret;
    DB_RESULT result;
    DB_ROW row;
    zbx_uint64_t userid;
    char *jobnet_id;
    int user_type, public_flag, user_cmp;
    const char *__function_name = "job_exec_auth";

    zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
    public_flag = 0;
    user_cmp = 0;
    userid = ja_user_auth(er.username, er.password);
    if (userid == 0) {
        ja_log("JATRAPPER200008", 0, er.jobnetid, 0);
        return FAIL;
    }
    if (ja_user_status(userid) != 0) {
        ja_log("JATRAPPER200050", 0, er.jobnetid, 0, er.username);
        return FAIL;
    }
    user_type = ja_user_type(userid);

    ret = SUCCEED;
    jobnet_id = DBdyn_escape_string(er.jobnetid);
    result =
        DBselect
        ("select user_name, public_flag from ja_jobnet_control_table where jobnet_id = '%s' and valid_flag = 1",
         jobnet_id);
    row = DBfetch(result);
    if (row == NULL) {
        ja_log("JATRAPPER200046", 0, jobnet_id, 0, jobnet_id);
        ret = FAIL;
    } else {
        public_flag = atoi(row[1]);
        if (ja_user_groups(ja_user_id(row[0]), userid) > 0)
            user_cmp = 1;
    }
    zbx_free(jobnet_id);
    DBfree_result(result);

    if (ret == FAIL)
        return FAIL;
    if (user_type == USER_TYPE_SUPER_ADMIN || public_flag == 1
        || user_cmp == 1)
        return SUCCEED;
    ja_log("JATRAPPER200047", 0, jobnet_id, 0, er.jobnetid);
    return FAIL;
}
Exemplo n.º 4
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int ja_host_lock(const char *host, const zbx_uint64_t inner_job_id)
{
    int db_ret;
    char *host_esc;
    const char *__function_name = "ja_host_lock";

    zabbix_log(LOG_LEVEL_DEBUG, "In %s() host: %s", __function_name, host);

    if (ja_host_getip(host, NULL, inner_job_id, NULL, JA_TXN_ON) == 0)
        return FAIL;

    DBfree_result(DBselect
                  ("select lock_host_name from ja_host_lock_table where lock_host_name = 'HOST_LOCK_RECORD' for update"));

    host_esc = DBdyn_escape_string(host);
    db_ret =
        DBexecute
        ("insert into ja_host_lock_table (lock_host_name) values ('%s')",
         host_esc);
    zbx_free(host_esc);

    if (db_ret < ZBX_DB_OK) {
        ja_log("JAHOST200005", 0, NULL, inner_job_id, __function_name, host, inner_job_id);
        return FAIL;
    } else {
        return SUCCEED;
    }
}
Exemplo n.º 5
0
/******************************************************************************
 *                                                                            *
 * Function: ja_connect_to_port                                               *
 *                                                                            *
 * Purpose: connection to the specified port on the host                      *
 *                                                                            *
 * Parameters: s            (in)  - socket identification information         *
 *             host         (in)  - host name                                 *
 *             inner_job_id (in)  - inner job id                              *
 *             txn          (in)  - transaction instruction                   *
 *                                                                            *
 * Return value: SUCCEED - normal end                                         *
 *               FAIL    - an error occurred                                  *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int ja_connect_to_port(zbx_sock_t * s, const char *host, const zbx_uint64_t inner_job_id, int txn)
{
    int ret;
    char host_ip[128];
    zbx_uint64_t hostid;
    int port;
    const char *__function_name = "ja_connect_to_port";

    zabbix_log(LOG_LEVEL_DEBUG, "In %s() host: %s", __function_name, host);

    hostid = ja_host_getip(host, host_ip, inner_job_id, &port, txn);
    if (hostid == 0) {
        return FAIL;
    }

    port = ja_host_getport(hostid, 1);

    zabbix_log(LOG_LEVEL_DEBUG, "In %s() connect the host. source_ip: %s, host_ip: %s, port: %d, timeout: %d",
               __function_name, CONFIG_SOURCE_IP, host_ip, port, CONFIG_TIMEOUT);

    ret = zbx_tcp_connect(s, CONFIG_SOURCE_IP, host_ip, port, CONFIG_TIMEOUT);
    if (ret == FAIL) {
        ja_log("JACONNECT300001", 0, NULL, inner_job_id, __function_name, zbx_tcp_strerror(),
               host_ip, port, CONFIG_SOURCE_IP, CONFIG_TIMEOUT);
    }

    return ret;
}
Exemplo n.º 6
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int jarun_icon_info(const zbx_uint64_t inner_job_id)
{
    DB_RESULT result;
    DB_ROW row;
    int status;
    char str_status[4];
    zbx_uint64_t inner_jobnet_id;
    int info_flag;
    char *get_job_id, *get_calendar_id;
    const char *__function_name = "jarun_icon_info";

    zabbix_log(LOG_LEVEL_DEBUG, "In %s() inner_job_id: " ZBX_FS_UI64,
               __function_name, inner_job_id);

    result =
        DBselect
        ("select inner_jobnet_id, info_flag, get_job_id, get_calendar_id"
         " from ja_run_icon_info_table" " where inner_job_id = "
         ZBX_FS_UI64, inner_job_id);

    status = -1;
    if (NULL != (row = DBfetch(result))) {
        ZBX_STR2UINT64(inner_jobnet_id, row[0]);
        info_flag = atoi(row[1]);
        get_job_id = row[2];
        get_calendar_id = row[3];
        switch (info_flag) {
        case 0:
            status = jarun_icon_info_get_status(inner_jobnet_id, get_job_id, inner_job_id);
            break;
        case 3:
            status = jarun_icon_info_get_calendar(get_calendar_id, inner_job_id);
            break;
        default:
            break;
        }
    } else {
        ja_log("JARUNICONINFO200003", inner_jobnet_id, NULL, inner_job_id,
               __function_name, inner_job_id);
        DBfree_result(result);
        return FAIL;
    }
    DBfree_result(result);
    if (status < 0)
        return ja_set_runerr(inner_job_id, 2);

    zbx_snprintf(str_status, sizeof(str_status), "%d", status);
    ja_set_value_after(inner_job_id, inner_jobnet_id, "LAST_STATUS",
                       str_status);

    return ja_flow(inner_job_id, JA_FLOW_TYPE_NORMAL, 1);
}
Exemplo n.º 7
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
void reply_jobnetstatusrq_response(zbx_sock_t * sock, int ret,
                                   JOBARG_JOBNET_INFO * ji, char *message)
{
    struct zbx_json json;

    /*JSON Data */
    zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN);
    zbx_json_addstring(&json, JA_PROTO_TAG_KIND,
                       JA_PROTO_VALUE_JOBNETSTATUSRQ_RES,
                       ZBX_JSON_TYPE_STRING);
    zbx_json_adduint64(&json, JA_PROTO_TAG_VERSION, JA_PROTO_VALUE_VERSION_1);  /*data version */
    zbx_json_addstring(&json, JA_PROTO_TAG_SERVERID, serverid,
                       ZBX_JSON_TYPE_STRING);
    zbx_json_addobject(&json, JA_PROTO_TAG_DATA);
    zbx_json_adduint64(&json, JA_PROTO_TAG_RESULT, FAIL == ret ? 1 : 0);
    if (ret != FAIL) {
        zbx_json_addstring(&json, JA_PROTO_TAG_JOBNETID, ji->jobnetid,
                           ZBX_JSON_TYPE_STRING);
        zbx_json_addstring(&json, JA_PROTO_TAG_JOBNETNAME, ji->jobnetname,
                           ZBX_JSON_TYPE_STRING);
        zbx_json_adduint64(&json, JA_PROTO_TAG_SCHEDULEDTIME,
                           ji->scheduled_time);
        zbx_json_adduint64(&json, JA_PROTO_TAG_STARTTIME, ji->start_time);
        zbx_json_adduint64(&json, JA_PROTO_TAG_ENDTIME, ji->end_time);
        zbx_json_adduint64(&json, JA_PROTO_TAG_JOBNETRUNTYPE,
                           ji->jobnetruntype);
        zbx_json_adduint64(&json, JA_PROTO_TAG_JOBNETSTATUS,
                           ji->jobnetstatus);
        zbx_json_adduint64(&json, JA_PROTO_TAG_JOBSTATUS, ji->jobstatus);
        zbx_json_addstring(&json, JA_PROTO_TAG_LASTEXITCD, ji->lastexitcd,
                           ZBX_JSON_TYPE_STRING);
        zbx_json_addstring(&json, JA_PROTO_TAG_LASTSTDOUT, ji->laststdout,
                           ZBX_JSON_TYPE_STRING);
        zbx_json_addstring(&json, JA_PROTO_TAG_LASTSTDERR, ji->laststderr,
                           ZBX_JSON_TYPE_STRING);
    } else {
        zbx_json_addstring(&json, JA_PROTO_TAG_MESSAGE, message,
                           ZBX_JSON_TYPE_STRING);
    }
    zbx_json_close(&json);
    zbx_json_close(&json);
    zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer);
    if (FAIL == (ret = zbx_tcp_send_to(sock, json.buffer, CONFIG_TIMEOUT))) {
        zbx_free(ji->jobnetid);
        zbx_free(ji->jobnetname);
        ja_log("JATRAPPER200038", 0, NULL, 0, zbx_tcp_strerror());
        ret = NETWORK_ERROR;
    }
    zbx_free(ji->jobnetid);
    zbx_free(ji->jobnetname);
    zbx_json_free(&json);
}
Exemplo n.º 8
0
void zbx_sigusr_handler(zbx_task_t task)
{
	switch (task) {
	case ZBX_TASK_CONFIG_CACHE_RELOAD:
		if (ZBX_PROCESS_TYPE_CONFSYNCER == process_type) {
			ja_log("JASERVER300001", 0, NULL, 0);
			ja_wakeup();
		}
		break;
	default:
		break;
	}
}
Exemplo n.º 9
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int ja_host_auth(zbx_sock_t * sock, const char *host, const zbx_uint64_t inner_job_id)
{
    char host_ip[128];
    const char *__function_name = "ja_host_auth";

    zabbix_log(LOG_LEVEL_DEBUG, "In %s() host: %s", __function_name, host);

    if (ja_host_getip(host, host_ip, inner_job_id, NULL, JA_TXN_ON) == 0) {
        return FAIL;
    }

    if (zbx_tcp_check_security(sock, host_ip, 0) == FAIL) {
        ja_log("JAHOST200006", 0, NULL, inner_job_id, __function_name, host, host_ip, inner_job_id);
        return FAIL;
    }
    return SUCCEED;
}
Exemplo n.º 10
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
static void jatraper_reply(zbx_sock_t * sock, ja_telegram_object * obj)
{
    char *data;
    const char *__function_name = "jatraper_reply";
    zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);

    json_object_object_add(obj->response, JA_PROTO_TAG_KIND,
                           json_object_new_string("response"));
    json_object_object_add(obj->response, JA_PROTO_TAG_VERSION,
                           json_object_new_int(JA_PROTO_TELE_VERSION));
    json_object_object_add(obj->response, JA_PROTO_TAG_SERVERID,
                           json_object_new_string(serverid));
    data = (char *) json_object_to_json_string(obj->response);

    zabbix_log(LOG_LEVEL_DEBUG, "In %s() %s", __function_name, data);
    if (zbx_tcp_send_to(sock, data, CONFIG_TIMEOUT) == FAIL) {
        ja_log("JATRAPPER200038", 0, NULL, 0, zbx_tcp_strerror());
    }
}
Exemplo n.º 11
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int jarun_icon_info_get_calendar(const char *get_calendar_id, const zbx_uint64_t inner_job_id)
{
    int status;
    char today[9];
    DB_RESULT result;
    DB_ROW row;
    char *update_date;
    const char *__function_name = "jarun_icon_info_get_calendar";

    zabbix_log(LOG_LEVEL_DEBUG, "In %s() get_calendar_id: %s",
               __function_name, get_calendar_id);
    status = -1;
    update_date = NULL;
    zbx_snprintf(today, sizeof(today), "%s", ja_timestamp2str(time(NULL)));

    result =
        DBselect
        ("select update_date from ja_calendar_control_table where calendar_id = '%s' and valid_flag = 1",
         get_calendar_id);
    if (NULL != (row = DBfetch(result))) {
        update_date = zbx_strdup(NULL, row[0]);
    }
    DBfree_result(result);
    if (update_date == NULL) {
        ja_log("JARUNICONINFO200004", 0, NULL, inner_job_id,
               __function_name, get_calendar_id);
        return -1;
    }
    result =
        DBselect
        ("select operating_date from ja_calendar_detail_table where calendar_id = '%s' and update_date = %s and operating_date = %s",
         get_calendar_id, update_date, today);
    if (NULL != (row = DBfetch(result))) {
        status = 1;
    } else {
        status = 0;
    }
    DBfree_result(result);

    zbx_free(update_date);
    return status;
}
Exemplo n.º 12
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
void main_jatrapper_loop(zbx_sock_t * s)
{
    const char *__function_name = "main_jatrapper_loop";
    zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);

    zbx_setproctitle("%s [connecting to the database]",
                     ja_get_process_type_string(process_type));
    for (;;) {
        zbx_setproctitle("%s [waiting for connection]",
                         ja_get_process_type_string(process_type));
        ja_update_selfmon_counter(ZBX_PROCESS_STATE_IDLE);
        if (SUCCEED == zbx_tcp_accept(s)) {
            ja_update_selfmon_counter(ZBX_PROCESS_STATE_BUSY);
            zbx_setproctitle("%s [processing data]",
                             get_process_type_string(process_type));
            process_jatrapper(s);
            zbx_tcp_unaccept(s);
        } else {
            ja_log("JATRAPPER200039", 0, NULL, 0);
        }
    }
}
Exemplo n.º 13
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
void zbx_on_exit()
{
	int	rc;
	zabbix_log(LOG_LEVEL_DEBUG, "zbx_on_exit() called");

	if (SUCCEED == DBtxn_ongoing())
		DBrollback();

	if (NULL != threads) {
		int		i;
		sigset_t	set;

		/* ignore SIGCHLD signals in order for zbx_sleep() to work */
		sigemptyset(&set);
		sigaddset(&set, SIGCHLD);
		sigprocmask(SIG_BLOCK, &set, NULL);

		for (i = 0; i < threads_num; i++) {
			if (threads[i]) {
				kill(threads[i], SIGTERM);
				threads[i] = ZBX_THREAD_HANDLE_NULL;
			}
		}

		zbx_free(threads);
	}

	zbx_sleep(2);	/* wait for all child processes to exit */

	ja_free_selfmon_collector();

	/* kill the external command */
	rc = system("pkill -SIGTERM jobarg_session");
	rc = system("pkill -SIGTERM jobarg_command");

	ja_log("JASERVER000002", 0, NULL, 0, JOBARG_VERSION, JOBARG_REVISION);
	zabbix_close_log();
	exit(SUCCEED);
}
Exemplo n.º 14
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int ja_setenv(const zbx_uint64_t inner_job_id)
{
    int ret;
    DB_RESULT result;
    DB_ROW row;
    char *key, *value;
    const char *__function_name = "ja_setenv";

    zabbix_log(LOG_LEVEL_DEBUG,
               "In %s() inner_job_id: " ZBX_FS_UI64,
               __function_name, inner_job_id);

    result =
        DBselect
        ("select value_name, before_value from ja_run_value_before_table"
         " where inner_job_id = " ZBX_FS_UI64, inner_job_id);

    ret = SUCCEED;
    while (NULL != (row = DBfetch(result))) {
        key = row[0];
        value = row[1];

        zabbix_log(LOG_LEVEL_DEBUG,
                   "In %s() inner_job_id: " ZBX_FS_UI64
                   ",  %s = %s", __function_name,
                   inner_job_id, key, value);

        if (setenv(key, value, 1) != 0) {
            ja_log("JAENV300001", 0, NULL, inner_job_id, __function_name,
                   inner_job_id, key, value);
            ret = FAIL;
            break;
        }
    }
    DBfree_result(result);
    return ret;
}
Exemplo n.º 15
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
void reply_jobnetrun_response(zbx_sock_t * sock, int ret, char *message)
{
    struct zbx_json json;

    /*JSON Data */
    zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN);
    zbx_json_addstring(&json, JA_PROTO_TAG_KIND,
                       JA_PROTO_VALUE_JOBNETRUN_RES, ZBX_JSON_TYPE_STRING);
    zbx_json_adduint64(&json, JA_PROTO_TAG_VERSION, JA_PROTO_VALUE_VERSION_1);  /*data version */
    zbx_json_addstring(&json, JA_PROTO_TAG_SERVERID, serverid,
                       ZBX_JSON_TYPE_STRING);
    zbx_json_addobject(&json, JA_PROTO_TAG_DATA);
    zbx_json_adduint64(&json, JA_PROTO_TAG_RESULT, FAIL == ret ? 1 : 0);
    zbx_json_addstring(&json, JA_PROTO_TAG_MESSAGE, message,
                       ZBX_JSON_TYPE_STRING);
    zbx_json_close(&json);
    zbx_json_close(&json);
    zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer);
    if (FAIL == (ret = zbx_tcp_send_to(sock, json.buffer, CONFIG_TIMEOUT))) {
        ja_log("JATRAPPER200038", 0, NULL, 0, zbx_tcp_strerror());
        ret = NETWORK_ERROR;
    }
    zbx_json_free(&json);
}
Exemplo n.º 16
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int jajobnet_ready(const zbx_uint64_t inner_jobnet_id)
{
    DB_RESULT result;
    DB_ROW row;
    zbx_uint64_t icon_start_id;
    int count;
    char time_str[13];
    const char *__function_name = "jajobnet_ready";

    zabbix_log(LOG_LEVEL_DEBUG,
               "In %s() inner_jobnet_id: " ZBX_FS_UI64, __function_name,
               inner_jobnet_id);

    // set jobnet before value
    result =
        DBselect
        ("select jobnet_id, jobnet_name, user_name, inner_jobnet_id from ja_run_jobnet_table"
         " where inner_jobnet_id = " ZBX_FS_UI64, inner_jobnet_id);

    if (NULL != (row = DBfetch(result))) {
        ja_set_value_jobnet_before(inner_jobnet_id, "JOBNET_ID", row[0]);
        ja_set_value_jobnet_before(inner_jobnet_id, "JOBNET_NAME", row[1]);
        ja_set_value_jobnet_before(inner_jobnet_id, "USER_NAME", row[2]);
        ja_set_value_jobnet_before(inner_jobnet_id, "MANAGEMENT_ID", row[3]);
    } else {
        ja_log("JAJOBNETREADY200001", inner_jobnet_id, NULL, 0,
               __function_name, inner_jobnet_id);
        DBfree_result(result);
        return ja_set_enderr_jobnet(inner_jobnet_id);
    }
    DBfree_result(result);
    zbx_snprintf(time_str, sizeof(time_str), "%s",
                 ja_timestamp2str(time(NULL)));
    ja_set_value_jobnet_before(inner_jobnet_id, "JOBNET_BOOT_TIME", time_str);


    // set jobnet status
    if (ja_set_run_jobnet(inner_jobnet_id) == FAIL)
        return FAIL;

    // search start icon
    count = 0;
    icon_start_id = 0;
    result =
        DBselect
        ("select inner_job_id from ja_run_job_table"
         " where inner_jobnet_id = " ZBX_FS_UI64 " and job_type = %d ",
         inner_jobnet_id, JA_JOB_TYPE_START);
    while (NULL != (row = DBfetch(result))) {
        count++;
        if (count == 1)
            ZBX_STR2UINT64(icon_start_id, row[0]);
        else
            icon_start_id = 0;
    }
    DBfree_result(result);

    if (icon_start_id == 0) {
        ja_log("JAJOBNETREADY200002", inner_jobnet_id, NULL, 0,
               __function_name, count, inner_jobnet_id);
        return ja_set_enderr_jobnet(inner_jobnet_id);
    } else {
        if (ja_value_before_jobnet_out(inner_jobnet_id, icon_start_id) ==
            FAIL)
            return FAIL;
        if (ja_set_status_job
            (icon_start_id, JA_JOB_STATUS_READY, 0, 0) == FAIL)
            return FAIL;
    }
    return SUCCEED;
}
Exemplo n.º 17
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int MAIN_ZABBIX_ENTRY()
{
	pid_t		pid;
	zbx_sock_t	listen_sock;
	int		i, server_num = 0, server_count = 0;

	if (NULL == CONFIG_LOG_FILE || '\0' == *CONFIG_LOG_FILE)
		zabbix_open_log(LOG_TYPE_SYSLOG, CONFIG_LOG_LEVEL, NULL);
	else
		zabbix_open_log(LOG_TYPE_FILE, CONFIG_LOG_LEVEL, CONFIG_LOG_FILE);

	ja_log("JASERVER000001", 0, NULL, 0, JOBARG_VERSION, JOBARG_REVISION);

	ja_init_selfmon_collector();
	threads_num = CONFIG_JARUN_FORKS + CONFIG_JATRAPPER_FORKS + CONFIG_JAJOB_FORKS
			+ CONFIG_JAJOBNET_FORKS + CONFIG_JALOADER_FORKS + CONFIG_JABOOT_FORKS
			+ CONFIG_JAMSGSND_FORKS + CONFIG_JASELFMON_FORKS;
	threads = zbx_calloc(threads, threads_num, sizeof(pid_t));

	if (FAIL == zbx_tcp_listen(&listen_sock, CONFIG_LISTEN_IP, (unsigned short) CONFIG_LISTEN_PORT)) {
		ja_log("JASERVER100001", 0, NULL, 0, zbx_tcp_strerror());
		exit(1);
	}

	for (i = 0; i < threads_num; i++) {
		if (0 == (pid = zbx_child_fork())) {
			server_num = i + 1;
			break;
		} else
			threads[i] = pid;
	}

	if (0 == server_num) {
		ja_log("JASERVER000003", 0, NULL, 0);

		while (-1 == wait(&i)) {
			if (EINTR != errno) {
				ja_log("JASERVER200001", 0, NULL, 0, zbx_strerror(errno));
				break;
			}
		}

		THIS_SHOULD_NEVER_HAPPEN;
		zbx_on_exit();
	} else if (server_num <= (server_count += CONFIG_JAJOBNET_FORKS)) {
		INIT_SERVER(JA_PROCESS_TYPE_JAJOBNET, CONFIG_JAJOBNET_FORKS);
		main_jajobnet_loop();
	} else if (server_num <= (server_count += CONFIG_JARUN_FORKS)) {
		INIT_SERVER(JA_PROCESS_TYPE_JARUN, CONFIG_JARUN_FORKS);
		main_jarun_loop();
	} else if (server_num <= (server_count += CONFIG_JAJOB_FORKS)) {
		INIT_SERVER(JA_PROCESS_TYPE_JAJOB, CONFIG_JAJOB_FORKS);
		main_jajob_loop();
	} else if (server_num <= (server_count += CONFIG_JATRAPPER_FORKS)) {
		INIT_SERVER(JA_PROCESS_TYPE_JATRAPPER, CONFIG_JATRAPPER_FORKS);
		main_jatrapper_loop(&listen_sock);
	} else if (server_num <= (server_count += CONFIG_JALOADER_FORKS)) {
		INIT_SERVER(JA_PROCESS_TYPE_JALOADER, CONFIG_JALOADER_FORKS);
		main_jaloader_loop();
	} else if (server_num <= (server_count += CONFIG_JABOOT_FORKS)) {
		INIT_SERVER(JA_PROCESS_TYPE_JABOOT, CONFIG_JABOOT_FORKS);
		main_jaboot_loop();
	} else if (server_num <= (server_count += CONFIG_JAMSGSND_FORKS)) {
		INIT_SERVER(JA_PROCESS_TYPE_JASNDMSG, CONFIG_JAMSGSND_FORKS);
		main_jamsgsnd_loop();
	} else if (server_num <= (server_count += CONFIG_JASELFMON_FORKS)) {
		INIT_SERVER(JA_PROCESS_TYPE_SELFMON, CONFIG_JASELFMON_FORKS);
		main_jaselfmon_loop();
	}

	return SUCCEED;
}
Exemplo n.º 18
0
/******************************************************************************
 *                                                                            *
 * Function: ja_host_getip                                                    *
 *                                                                            *
 * Purpose: get the ip address and port of the host                           *
 *                                                                            *
 * Parameters: host         (in)  - host name                                 *
 *             host_ip      (out) - ip address                                *
 *             inner_job_id (in)  - inner job id                              *
 *             port         (out) - port                                      *
 *             txn          (in)  - transaction instruction                   *
 *                                                                            *
 * Return value: return the host id. failure is zero                          *
 *                                                                            *
 * Comments: value is not set when host_ip or port is NULL                    *
 *                                                                            *
 ******************************************************************************/
zbx_uint64_t ja_host_getip(const char *host, char *host_ip, const zbx_uint64_t inner_job_id, int *port, int txn)
{
    DB_RESULT result;
    DB_RESULT result2;
    DB_ROW row;
    DB_ROW row2;
    char *host_esc;
    zbx_uint64_t hostid;
    const char *__function_name = "ja_host_getip";

    zabbix_log(LOG_LEVEL_DEBUG, "In %s() host: %s", __function_name, host);

    hostid = 0;
    result = NULL;
    host_esc = DBdyn_escape_string(host);
    switch (CONFIG_ZABBIX_VERSION) {
    case 1:
        // for zabbix 1.8
        result = DBselect("select hostid, useip, dns, ip, status, port from hosts where host = '%s'",
                          host_esc);
        break;
    case 2:
    case 3:
        // for zabbix 2.0 or 2.2
        result = DBselect(" select i.hostid, i.useip, i.dns, i.ip, h.status, i.port from hosts h, interface i"
                          " where h.hostid = i.hostid and i.main = 1 and h.host = '%s'",
                          host_esc);
        break;
    default:
        ja_log("JAHOST200001", 0, NULL, inner_job_id, __function_name, CONFIG_ZABBIX_VERSION);
        goto error;
    }

    if (result == NULL) {
        if (txn == JA_TXN_ON) {
            DBrollback();
        }
        ja_log("JAHOST200007", 0, NULL, inner_job_id, __function_name, CONFIG_ZABBIX_VERSION);
        goto error;
    }

    row = DBfetch(result);
    if (row == NULL) {
        ja_log("JAHOST200002", 0, NULL, inner_job_id, __function_name, host, inner_job_id);
        goto error;
    }

    /* get the force flag and job id */
    result2 = DBselect("select force_flag, job_id from ja_run_job_table where inner_job_id = " ZBX_FS_UI64,
                       inner_job_id);
    if (NULL == (row2 = DBfetch(result2))) {
        ja_log("JAHOST200004", 0, NULL, inner_job_id, __function_name, inner_job_id);
        DBfree_result(result2);
        goto error;
    }

    if (atoi(row[4]) != HOST_STATUS_MONITORED && atoi(row2[0]) == JA_JOB_FORCE_FLAG_OFF) {
        ja_log("JAHOST200003", 0, NULL, inner_job_id, __function_name, host, inner_job_id, row2[1]);
        DBfree_result(result2);
        goto error;
    }

    DBfree_result(result2);

    ZBX_STR2UINT64(hostid, row[0]);
    if (host_ip != NULL) {
        if (atoi(row[1]) == 0) {
            // use dns
            zbx_snprintf(host_ip, strlen(row[2]) + 1, "%s", row[2]);
        } else {
            // use ip
            zbx_snprintf(host_ip, strlen(row[3]) + 1, "%s", row[3]);
        }
    }

    /* get port */
    if (port != NULL) {
        *port = atoi(row[5]);
    }

    DBfree_result(result);
    result = NULL;

  error:
    zbx_free(host_esc);
    if (result != NULL)
        DBfree_result(result);
    return hostid;
}
Exemplo n.º 19
0
/******************************************************************************
 *                                                                            *
 * Function: ja_zabbix_message                                                *
 *                                                                            *
 * Purpose: get Zabbix status change message body                             *
 *                                                                            *
 * Parameters: message_id (in) - message id                                   *
 *             lang (in) - message language                                   *
 *             inner_job_id (in) - inner job id                               *
 *                                                                            *
 * Return value:  pointer of the message body                                 *
 *                NULL - an error occurred                                    *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
char *ja_zabbix_message(char *message_id, char *lang, zbx_uint64_t inner_job_id)
{
	FILE		*fp;
	int		hit, m, n, cnt;
	char		*name = NULL, *mlang = NULL, *msg = NULL, *p = NULL;
	char		line[4096];


	/* zabbix message file open */
	fp = fopen(CONFIG_JA_ZBX_MESSAGE_FILE, "r");
	if (fp == NULL)
	{
		ja_log("JATRGMSG200001", 0, NULL, inner_job_id, CONFIG_JA_ZBX_MESSAGE_FILE);
		return NULL;
	}

	/* message get */
	cnt = 0;
	hit = 0;
	while (fgets(line, sizeof(line), fp) != NULL)
	{
		cnt = cnt + 1;

		if (line[0] == '#' || line[0] == '\n' || line[0] == '\r' )
		{
			continue;
		}

		if (strlen(line) > 0)
		{
			if (line[strlen(line)-1] == '\n')
			{
				line[strlen(line)-1] = '\0';
			}
		}

		if (strlen(line) > 0)
		{
			if (line[strlen(line)-1] == '\r')
			{
				line[strlen(line)-1] = '\0';
			}
		}

		n = 0;
		m = 0;
		name  = line;
		mlang = NULL;
		msg   = NULL;
		while (line[++n])
		{
			if (line[n] == ',')
			{
				if (m == 0)
				{
					line[n] = '\0';
					mlang = &line[n + 1];
					m++;
				}
				else if (m == 1)
				{
					line[n] = '\0';
					msg = &line[n + 1];
					break;
				}
			}
		}

		lrtrim_spaces(name);
		lrtrim_spaces(mlang);

		/* convert all characters to lower case */
		for (p = mlang; *p != '\0'; p++) {
			*p = tolower(*p);
		}

		for (p = lang; *p != '\0'; p++) {
			*p = tolower(*p);
		}

		if (strcmp(name, message_id) == 0 && strcmp(mlang, lang) == 0)
		{
			hit = 1;
			break;
		}
	}
	fclose(fp);

	/* message hit check */
	if (hit == 0)
	{
		ja_log("JATRGMSG200002", 0, NULL, inner_job_id, message_id, lang);
		return NULL;
	}

	/* message get check */
	if (name == NULL || mlang == NULL || msg == NULL)
	{
		ja_log("JATRGMSG200003", 0, NULL, inner_job_id, cnt, message_id, CONFIG_JA_ZBX_MESSAGE_FILE);
		return NULL;
	}

	return DBdyn_escape_string(msg);

}
Exemplo n.º 20
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int jarun_icon_extjob(const zbx_uint64_t inner_job_id, const int test_flag)
{
    int ret;
    DB_RESULT result;
    DB_ROW row;
    zbx_uint64_t inner_jobnet_id, inner_jobnet_main_id;
    char command_id[JA_MAX_STRING_LEN], value[JA_MAX_STRING_LEN],
        start_time[16], command[JA_MAX_DATA_LEN];
    const char *__function_name = "jarun_icon_extjob";

    zabbix_log(LOG_LEVEL_DEBUG, "In %s() inner_job_id: " ZBX_FS_UI64,
               __function_name, inner_job_id);

    result =
        DBselect
        ("select inner_jobnet_id, command_id, value from ja_run_icon_extjob_table"
         " where inner_job_id = " ZBX_FS_UI64, inner_job_id);
    row = DBfetch(result);
    if (row == NULL) {
        ja_log("JARUNICONEXTJOB200004", 0, NULL, inner_job_id,
               __function_name, inner_job_id);
        DBfree_result(result);
        return ja_set_runerr(inner_job_id);
    }

    ZBX_STR2UINT64(inner_jobnet_id, row[0]);
    zbx_snprintf(command_id, sizeof(command_id), "%s", row[1]);
    ja_format_extjob(row[2], value);
    DBfree_result(result);

    result =
        DBselect
        ("select inner_jobnet_main_id from ja_run_jobnet_table"
         " where inner_jobnet_id = " ZBX_FS_UI64, inner_jobnet_id);
    row = DBfetch(result);
    if (row == NULL) {
        ja_log("JARUNICONEXTJOB200007", 0, NULL, inner_job_id,
               __function_name, inner_jobnet_id);
        DBfree_result(result);
        return ja_set_runerr(inner_job_id);
    }
    ZBX_STR2UINT64(inner_jobnet_main_id, row[0]);
    DBfree_result(result);

    if (test_flag == JA_JOB_TEST_FLAG_ON) {
        zbx_snprintf(command, sizeof(command), " ");
    } else {
        if (strcmp(command_id, JA_CMD_TIME) == 0) {
            if (ja_get_jobnet_summary_start(inner_jobnet_main_id, start_time) ==
                FAIL) {
                ja_log("JARUNICONEXTJOB200005", inner_jobnet_main_id, NULL,
                       inner_job_id, __function_name, inner_jobnet_main_id);
                return ja_set_runerr(inner_job_id);
            }
            zbx_snprintf(command, sizeof(command), "%s%c%s %s %s",
                         CONFIG_EXTJOB_PATH, JA_DLM, command_id, value,
                         start_time);
        } else {
            zbx_snprintf(command, sizeof(command), "%s%c%s %s",
                         CONFIG_EXTJOB_PATH, JA_DLM, command_id, value);
        }
    }
    ret = jarun_icon_extjob_run(inner_job_id, inner_jobnet_id, command);

    zabbix_log(LOG_LEVEL_DEBUG, "End of %s() inner_job_id: " ZBX_FS_UI64,
               __function_name, inner_job_id);
    return ret;
}
Exemplo n.º 21
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int jarun_icon_extjob_run(const zbx_uint64_t inner_job_id,
                          const zbx_uint64_t inner_jobnet_id,
                          const char *command)
{
    int ret, db_ret;
    pid_t pid;
    char filepath[JA_MAX_STRING_LEN];
    char full_command[JA_MAX_STRING_LEN];
    char jacmd[JA_MAX_STRING_LEN];
    const char *__function_name = "jarun_icon_extjob_run";

    zabbix_log(LOG_LEVEL_DEBUG,
               "In %s() inner_job_id: " ZBX_FS_UI64
               ", command: %s", __function_name, inner_job_id, command);

    ret = SUCCEED;
    zbx_snprintf(filepath, JA_MAX_STRING_LEN, "%s%c%s-" ZBX_FS_UI64,
                 CONFIG_TMPDIR, JA_DLM, progname, inner_job_id);
    zbx_snprintf(full_command, JA_MAX_STRING_LEN, "%s.%s", filepath,
                 JA_EXE);
    zbx_snprintf(jacmd, JA_MAX_STRING_LEN, "%s%c%s", CONFIG_EXTJOB_PATH,
                 JA_DLM, JA_JOBARG_COMMAND);
    if (ja_jobfile_create(filepath, jobext, command) != SUCCEED) {
        return ja_set_runerr(inner_job_id);
    }

    pid = ja_fork();
    if (pid == -1) {
        ja_log("JARUNICONEXTJOB200001", inner_jobnet_id, NULL,
               inner_job_id, __function_name);
        return ja_set_runerr(inner_job_id);
    } else if (pid != 0) {
        waitpid(pid, NULL, WNOHANG);
        db_ret =
            DBexecute
            ("update ja_run_icon_extjob_table set pid = %d where inner_job_id = "
             ZBX_FS_UI64, pid, inner_job_id);
        if (db_ret < ZBX_DB_OK) {
            ja_log("JARUNICONEXTJOB200006", inner_jobnet_id, NULL,
                   inner_job_id, __function_name, pid, inner_job_id);
            return FAIL;
        }
        zabbix_log(LOG_LEVEL_DEBUG,
                   "End of %s() inner_job_id: " ZBX_FS_UI64,
                   __function_name, inner_job_id);
        return SUCCEED;
    }

    DBconnect(ZBX_DB_CONNECT_ONCE);
    if (ja_setenv(inner_job_id) == FAIL) {
        DBclose();
        goto error;
    }
    DBclose();
    execl(jacmd, JA_JOBARG_COMMAND, filepath, full_command, NULL);

  error:
    DBconnect(ZBX_DB_CONNECT_ONCE);
    ja_log("JARUNICONEXTJOB200003", inner_jobnet_id, NULL, inner_job_id,
           __function_name, jacmd, zbx_strerror(errno));
    ja_set_runerr(inner_job_id);
    DBclose();
    exit(0);
}
Exemplo n.º 22
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
char *evaluate_jobnetstatusrq(zbx_sock_t * sock,
                              struct zbx_json_parse *jp, int *ret,
                              JOBARG_JOBNET_INFO * ji)
{
    struct zbx_json_parse jp_row;
    char *message = NULL;
    char value[MAX_STRING_LEN];
    const char *p;
    int version;
    zbx_uint64_t registrynumber;
    static JOBARG_EXEC_REQUEST er;
    init_exec_request(&er);
    if (SUCCEED ==
        zbx_json_value_by_name(jp, JA_PROTO_TAG_VERSION, value,
                               sizeof(value))) {
        version = atoi(value);
        if (version != JA_PROTO_VALUE_VERSION_1) {
            ja_log("JATRAPPER200027", 0, NULL, 0,
                   JA_PROTO_VALUE_VERSION_1);
            *ret = FAIL;
            return zbx_dsprintf(message,
                                "Received message error: [version] is not correct.");
        }
    } else {
        ja_log("JATRAPPER200028", 0, NULL, 0);
        *ret = FAIL;
        return zbx_dsprintf(message,
                            "Received message error: [version] not found");
    }
    if (NULL == (p = zbx_json_pair_by_name(jp, JA_PROTO_TAG_DATA))) {
        ja_log("JATRAPPER200029", 0, NULL, 0);
        *ret = FAIL;
        return zbx_dsprintf(message,
                            "Received message error: [data] not found");
    } else {
        if (FAIL == (*ret = zbx_json_brackets_open(p, &jp_row))) {
            ja_log("JATRAPPER200030", 0, NULL, 0);
            *ret = FAIL;
            return zbx_dsprintf(message,
                                "Received message error: Cannot open [data] object");
        } else {
            if (SUCCEED ==
                zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_USERNAME,
                                       value, sizeof(value))) {
                er.username = strdup(value);
            } else {
                ja_log("JATRAPPER200032", 0, NULL, 0);
                *ret = FAIL;
                return zbx_dsprintf(message,
                                    "Received message error: [username] not found");
            }
            if (SUCCEED ==
                zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_PASSWORD,
                                       value, sizeof(value))) {
                er.password = strdup(value);
            } else {
                ja_log("JATRAPPER200033", 0, NULL, 0);
                zbx_free(er.username);
                *ret = FAIL;
                return zbx_dsprintf(message,
                                    "Received message error: [password] not found");
            }
            if (SUCCEED ==
                zbx_json_value_by_name(&jp_row,
                                       JA_PROTO_TAG_REGISTRYNUMBER,
                                       value, sizeof(value))) {
                ZBX_STR2UINT64(registrynumber, value);
            } else {
                ja_log("JATRAPPER200052", 0, NULL, 0);
                zbx_free(er.username);
                zbx_free(er.password);
                *ret = FAIL;
                return zbx_dsprintf(message,
                                    "Received message error: [registrynumber] not found");
            }
            if (SUCCEED != get_jobnet_info(registrynumber, &er, ji)) {
                zbx_free(er.username);
                zbx_free(er.password);
                *ret = FAIL;
                return zbx_dsprintf(message,
                                    "jobnet specified by the registry number is not found");
            }
            if (SUCCEED == (job_exec_auth(er))) {
                zbx_free(er.username);
                zbx_free(er.password);
                *ret = SUCCEED;
                return zbx_dsprintf(message, "OK.");
            } else {
                zbx_free(er.username);
                zbx_free(er.password);
                *ret = FAIL;
                return zbx_dsprintf(message, "Authentication failure.");
            }
        }
    }
}
Exemplo n.º 23
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
char *evaluate_jobnetrun(zbx_sock_t * sock, struct zbx_json_parse *jp,
                         int *ret)
{
    DB_RESULT result;
    DB_ROW row;
    struct zbx_json_parse jp_row;
    struct zbx_json_parse jp_row2;
    char *message = NULL;
    char value[MAX_STRING_LEN];
    const char *p;
    const char *p2;
    const char *p3 = NULL;
    int version;
    int res;
    int i;
    int count;
    zbx_uint64_t inner_jobnet_id;
    static JOBARG_EXEC_REQUEST er;
    init_exec_request(&er);
    if (SUCCEED ==
        zbx_json_value_by_name(jp, JA_PROTO_TAG_VERSION, value,
                               sizeof(value))) {
        version = atoi(value);
        if (version != JA_PROTO_VALUE_VERSION_1) {
            ja_log("JATRAPPER200027", 0, NULL, 0,
                   JA_PROTO_VALUE_VERSION_1);
            *ret = FAIL;
            return zbx_dsprintf(message,
                                "Received message error: [version] is not correct.");
        }
    } else {
        ja_log("JATRAPPER200028", 0, NULL, 0);
        *ret = FAIL;
        return zbx_dsprintf(message,
                            "Received message error: [version] not found");
    }
    if (NULL == (p = zbx_json_pair_by_name(jp, JA_PROTO_TAG_DATA))) {
        ja_log("JATRAPPER200021", 0, NULL, 0);
        *ret = FAIL;
        return zbx_dsprintf(message,
                            "Received message error: [data] not found");
    } else {
        if (FAIL == (*ret = zbx_json_brackets_open(p, &jp_row))) {
            ja_log("JATRAPPER200022", 0, NULL, 0);
            *ret = FAIL;
            return zbx_dsprintf(message,
                                "Received message error: Cannot open [data] object");
        } else {
            if (SUCCEED ==
                zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_USERNAME,
                                       value, sizeof(value))) {
                er.username = strdup(value);
            } else {
                ja_log("JATRAPPER200032", 0, NULL, 0);
                *ret = FAIL;
                return zbx_dsprintf(message,
                                    "Received message error: [username] not found");
            }
            if (SUCCEED ==
                zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_PASSWORD,
                                       value, sizeof(value))) {
                er.password = strdup(value);
            } else {
                ja_log("JATRAPPER200033", 0, NULL, 0);
                zbx_free(er.username);
                *ret = FAIL;
                return zbx_dsprintf(message,
                                    "Received message error: [password] not found");
            }
            if (SUCCEED ==
                zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_JOBNETID,
                                       value, sizeof(value))) {
                er.jobnetid = strdup(value);
            } else {
                ja_log("JATRAPPER200034", 0, NULL, 0);
                zbx_free(er.username);
                zbx_free(er.password);
                *ret = FAIL;
                return zbx_dsprintf(message,
                                    "Received message error: [jobnetid] not found");
            }
            if (SUCCEED ==
                zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_STARTTIME,
                                       value, sizeof(value))) {
                er.starttime = strdup(value);
                res = time_passed_check(er.starttime);
                if (res == FAIL) {
                    zbx_free(er.username);
                    zbx_free(er.password);
                    zbx_free(er.jobnetid);
                    zbx_free(er.starttime);
                    *ret = FAIL;
                    return zbx_dsprintf(message,
                                        "Received message error: [start_time] already passed.");
                }
            }
            i = 0;
            if (NULL !=
                (p2 = zbx_json_pair_by_name(&jp_row, JA_PROTO_TAG_ENV))) {
                if (FAIL == (*ret = zbx_json_brackets_open(p2, &jp_row2))) {
                    ja_log("JATRAPPER200051", 0, er.jobnetid, 0);
                    zbx_free(er.username);
                    zbx_free(er.password);
                    zbx_free(er.jobnetid);
                    zbx_free(er.starttime);
                    *ret = FAIL;
                    return zbx_dsprintf(message,
                                        "Received message error: Cannot open [env] object");
                } else {
                    while (NULL !=
                           (p3 =
                            zbx_json_pair_next(&jp_row2, p3, value,
                                               sizeof(value)))) {
                        er.env[i] = strdup(value);
                        zbx_json_value_by_name(&jp_row2, er.env[i], value,
                                               sizeof(value));
                        er.value[i] = strdup(value);
                        i++;
                    }
                    er.env[i] = '\0';
                }
            }
            if (SUCCEED ==
                zbx_json_value_by_name(&jp_row, JA_PROTO_TAG_DETERRENCE,
                                       value, sizeof(value))) {
                er.deterrence = atoi(value);
            }

            if (SUCCEED == (job_exec_auth(er))) {
                DBbegin();
                /* double check start-up suppression time specified */
                if (er.starttime != NULL && er.deterrence == 1) {
                    result = DBselect("select count(*) from ja_run_jobnet_table"
                                      " where scheduled_time = %s and jobnet_id = '%s' and run_type = %d",
                                      er.starttime, er.jobnetid, JA_JOBNET_RUN_TYPE_SCHEDULED);
                    if (NULL == (row = DBfetch(result))) {
                        zbx_snprintf(msgwork, sizeof(msgwork), "%s %s %d", er.starttime, er.jobnetid, JA_JOBNET_RUN_TYPE_SCHEDULED);
                        ja_log("JATRAPPER200057", 0, er.jobnetid, 0, "ja_run_jobnet_table", msgwork);
                        DBfree_result(result);
                        DBrollback();
                        clean_exec_request(&er, i);
                        *ret = FAIL;
                        return zbx_dsprintf(message, "ja_run_jobnet_table select error.");
                    }
                    count = atoi(row[0]);
                    DBfree_result(result);

                    if (count > 0) {
                        DBrollback();
                        clean_exec_request(&er, i);
                        *ret = FAIL;
                        return zbx_dsprintf(message, "Received message error: Double registration detection of time starting jobnet.");
                    }
                }

                if (SUCCEED ==
                    (register_db_table(er, &inner_jobnet_id, i))) {
                    DBcommit();
                    clean_exec_request(&er, i);
                    return zbx_dsprintf(message,
                                        "Registry number :  [" ZBX_FS_UI64
                                        "]", inner_jobnet_id);
                } else {
                    DBrollback();
                    clean_exec_request(&er, i);
                    *ret = FAIL;
                    return zbx_dsprintf(message, "ja_run_jobnet_table insert error.");
                }
            } else {
                clean_exec_request(&er, i);
                *ret = FAIL;
                return zbx_dsprintf(message, "Authentication failure.");
            }
        }
    }
}
Exemplo n.º 24
0
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
int register_db_table(JOBARG_EXEC_REQUEST er,
                      zbx_uint64_t * inner_jobnet_id, int cnt)
{
    const char *__function_name = "register_db_table";
    char *user_name = NULL;
    char *jobnet_name = NULL;
    char *memo = NULL;
    char *jobnetid;
    char *env;
    char *value;
    int ret = SUCCEED;
    int res;
    int public_flag = 0;
    int run_type;
    int multiple_start_up;
    int i;
    zbx_uint64_t next_id;
    zbx_uint64_t update_date;
    zbx_uint64_t scheduled_time;
    DB_RESULT result;
    DB_ROW row;
    zabbix_log(LOG_LEVEL_DEBUG, "In %s() jobnetid: [%s]",
               __function_name, er.jobnetid);
    jobnetid = DBdyn_escape_string(er.jobnetid);
    result =
        DBselect
        ("select update_date, public_flag, multiple_start_up, user_name, jobnet_name, memo"
         " from ja_jobnet_control_table" " where jobnet_id='%s'"
         " and valid_flag=1", jobnetid);
    row = DBfetch(result);
    if (row == NULL) {
        ja_log("JATRAPPER200014", 0, jobnetid, 0, jobnetid);
        zbx_free(jobnetid);
        DBfree_result(result);
        ret = FAIL;
        return ret;
    }
    ZBX_STR2UINT64(update_date, row[0]);
    public_flag = atoi(row[1]);
    multiple_start_up = atoi(row[2]);
    user_name = DBdyn_escape_string(row[3]);
    jobnet_name = DBdyn_escape_string(row[4]);
    memo = DBdyn_escape_string(row[5]);
    DBfree_result(result);
    if (er.starttime != NULL) {
        run_type = JA_JOBNET_RUN_TYPE_SCHEDULED;
        ZBX_STR2UINT64(scheduled_time, er.starttime);
    } else {
        run_type = JA_JOBNET_RUN_TYPE_IMMEDIATE;
        scheduled_time = 0;
    }
    next_id = get_next_id(JA_RUN_ID_JOBNET_EX, 0, jobnetid, 0);
    if (next_id == 0) {
        ja_log("JATRAPPER200015", 0, jobnetid, 0);
        zbx_free(jobnetid);
        zbx_free(user_name);
        zbx_free(jobnet_name);
        zbx_free(memo);
        ret = FAIL;
        return ret;
    }
    res = DBexecute("insert into ja_run_jobnet_table"
                    " (inner_jobnet_id, inner_jobnet_main_id,"
                    " update_date, run_type, main_flag, status, scheduled_time,"
                    " public_flag, multiple_start_up,"
                    " jobnet_id, user_name, jobnet_name, memo, execution_user_name)"
                    " values ( " ZBX_FS_UI64 ", " ZBX_FS_UI64 ", "
                    ZBX_FS_UI64 ", %d, %d, %d, " ZBX_FS_UI64
                    ", %d, %d,"
                    " '%s', '%s', '%s', '%s', '%s')",
                    next_id, next_id,
                    update_date, run_type, JA_JOBNET_MAIN_FLAG_MAIN, JA_JOBNET_STATUS_BEGIN, scheduled_time,
                    public_flag, multiple_start_up,
                    jobnetid, user_name, jobnet_name, memo, er.username);
    if (ZBX_DB_OK > res) {
        ja_log("JATRAPPER200016", 0, jobnetid, 0, jobnetid);
        zbx_free(jobnetid);
        zbx_free(user_name);
        zbx_free(jobnet_name);
        zbx_free(memo);
        ret = FAIL;
        return ret;
    }
    for (i = 0; i < cnt; i++) {
        env = NULL;
        value = NULL;
        env = DBdyn_escape_string(er.env[i]);
        value = DBdyn_escape_string(er.value[i]);
        res = DBexecute("insert into ja_value_before_jobnet_table"
                        " (inner_jobnet_id, value_name, before_value)"
                        " values ( " ZBX_FS_UI64 ", '%s', '%s' )",
                        next_id, env, value);
        if (ZBX_DB_OK > res) {
            ja_log("JATRAPPER200016", 0, jobnetid, 0, jobnetid);
            zbx_free(jobnetid);
            zbx_free(user_name);
            zbx_free(jobnet_name);
            zbx_free(memo);
            zbx_free(env);
            zbx_free(value);
            ret = FAIL;
            return ret;
        }
        zbx_free(env);
        zbx_free(value);
    }
    *inner_jobnet_id = next_id;
    zbx_free(jobnetid);
    zbx_free(user_name);
    zbx_free(jobnet_name);
    zbx_free(memo);
    zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
    return ret;
}