Exemplo n.º 1
0
int ldap_add_machine_account(const char *ldap_host, 
			     const char *hostname, const char *realm)
{
	LDAP *ld;
	int ldap_port = LDAP_PORT;
	char *bind_path;
	int rc;
	LDAPMessage *res;
	void *sasl_defaults;
	int version = LDAP_VERSION3;

	bind_path = build_dn(realm);

	printf("Creating host account for %s@%s\n", hostname, realm);

	ld = ldap_open(ldap_host, ldap_port);
	ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);

	rc = ldap_sasl_interactive_bind_s(ld, NULL, NULL, NULL, NULL, 0,
					  sasl_interact, NULL);

	if (rc != LDAP_SUCCESS) {
		ldap_perror(ld, "ldap_bind");
		goto failed;
	}

	rc = find_host(ld, &res, bind_path, hostname);
	if (rc == LDAP_SUCCESS && ldap_count_entries(ld, res) == 1) {
		printf("Host account for %s already exists\n", hostname);
		goto finished;
	}

	rc = add_host(ld, bind_path, hostname, realm);
	if (rc != LDAP_SUCCESS) {
		ldap_perror(ld, "add_host");
		goto failed;
	}

	rc = find_host(ld, &res, bind_path, hostname);
	if (rc != LDAP_SUCCESS || ldap_count_entries(ld, res) != 1) {
		ldap_perror(ld, "find_host test");
		goto failed;
	}

	printf("Successfully added machine account for %s\n", hostname);

finished:	
	free(bind_path);
	return 0;

failed:
	printf("ldap_add_machine_account failed\n");
	free(bind_path);
	ldap_unbind(ld);
	return 1;
}
Exemplo n.º 2
0
HostClass* get_hostclass(const string& ip, int firstnode) 
{
    if(HostClass* hc = find_host(hostlist, ip)) 
	return hc;

    HostClass hc(ip, firstnode);
    hostlist.push_back(hc);

    return find_host(hostlist, ip);
}
Exemplo n.º 3
0
/* determine what hosts are causing network outages */
void find_hosts_causing_outages(void) {
	hoststatus *temp_hoststatus;
	host *temp_host;

	/* check all hosts */
	for (temp_hoststatus = hoststatus_list; temp_hoststatus != NULL; temp_hoststatus = temp_hoststatus->next) {

		/* check only hosts that are not up and not pending */
		if (temp_hoststatus->status != HOST_UP && temp_hoststatus->status != HOST_PENDING) {

			/* find the host entry */
			temp_host = find_host(temp_hoststatus->host_name);

			if (temp_host == NULL)
				continue;

			if (is_authorized_for_host(temp_host, &current_authdata) == FALSE)
				continue;

			/* if the route to this host is not blocked, it is a causing an outage */
			if (is_route_to_host_blocked(temp_host) == FALSE)
				add_hostoutage(temp_host);
		}
	}

	return;
}
Exemplo n.º 4
0
void make_connection(struct connection *c, int port, int *sock, void (*func)(struct connection *))
{
	int as;
	unsigned char *host;
	struct conn_info *b;
	if (!(host = get_host_name(c->url))) {
		setcstate(c, S_INTERNAL);
		abort_connection(c);
		return;
	}
	if (c->newconn)
		internal("already making a connection");
	b = mem_alloc(sizeof(struct conn_info));
	b->func = func;
	b->sock = sock;
	b->port = port;
	c->newconn = b;
	log_data("\nCONNECTION: ", 13);
	log_data(host, strlen(host));
	log_data("\n", 1);
	if (c->no_cache >= NC_RELOAD) as = find_host_no_cache(host, &b->addr, &c->dnsquery, (void(*)(void *, int))dns_found, c);
	else as = find_host(host, &b->addr, &c->dnsquery, (void(*)(void *, int))dns_found, c);
	mem_free(host);
	if (as) setcstate(c, S_DNS);
}
Exemplo n.º 5
0
int register_service(service *new_service)
{

    host *h;
    g_return_val_if_fail(service_hash_table != NULL, ERROR);

    if (!(h = find_host(new_service->host_name))) {
        nm_log(NSLOG_CONFIG_ERROR, "Error: Unable to locate host '%s' for service '%s'\n",
               new_service->host_name, new_service->description);
        return ERROR;
    }

    if ((find_service(new_service->host_name, new_service->description))) {
        nm_log(NSLOG_CONFIG_ERROR, "Error: Service '%s' on host '%s' has already been defined\n", new_service->description, new_service->host_name);
        return ERROR;
    }

    g_hash_table_insert(service_hash_table,
                        nm_service_key_create(new_service->host_name, new_service->description), new_service);

    new_service->id = num_objects.services++;
    service_ary[new_service->id] = new_service;
    if (new_service->id)
        service_ary[new_service->id - 1]->next = new_service;
    else
        service_list = new_service;

    return OK;
}
Exemplo n.º 6
0
int is_host_in_pending_flex_downtime(struct host *temp_host)
{
	scheduled_downtime *temp_downtime = NULL;
	time_t current_time = 0L;

	log_debug_info(DEBUGL_FUNCTIONS, 0, "is_host_in_pending_flex_downtime()\n");

	if (temp_host == NULL)
		return FALSE;

	time(&current_time);

	/* check all downtime entries */
	for (temp_downtime = scheduled_downtime_list; temp_downtime != NULL; temp_downtime = temp_downtime->next) {
		if(temp_downtime->type != HOST_DOWNTIME)
			continue;
		if(temp_downtime->fixed == TRUE)
			continue;
		if(temp_downtime->is_in_effect == TRUE)
			continue;
		/* triggered downtime entries should be ignored here */
		if(temp_downtime->triggered_by != 0)
			continue;

		/* this entry matches our host! */
		if(find_host(temp_downtime->host_name) == temp_host) {
			/* if the time boundaries are okay, start this scheduled downtime */
			if(temp_downtime->start_time <= current_time && current_time <= temp_downtime->end_time)
				return TRUE;
		}
	}

	return FALSE;
}
Exemplo n.º 7
0
/* check if user is authorized to view information about all hosts in a particular hostgroup */
int is_authorized_for_hostgroup(hostgroup *hg, authdata *authinfo) {
	hostsmember *temp_hostsmember;
	host *temp_host;

	if(hg == NULL)
		return FALSE;

	/* CHANGED in 2.0 - user must be authorized for ALL hosts in a hostgroup, not just one */
	/* see if user is authorized for all hosts in the hostgroup */
	/*
	for(temp_hostsmember = hg->members; temp_hostsmember != NULL; temp_hostsmember = temp_hostsmember->next) {
		temp_host = find_host(temp_hostsmember->host_name);
		if(is_authorized_for_host(temp_host, authinfo) == FALSE)
			return FALSE;
		}
	*/
	/* Reverted for 3.3.2 - must only be a member of one hostgroup */
	for(temp_hostsmember = hg->members; temp_hostsmember != NULL; temp_hostsmember = temp_hostsmember->next) {
		temp_host = find_host(temp_hostsmember->host_name);
		if(is_authorized_for_host(temp_host, authinfo) == TRUE)
			return TRUE;
		}

	/*return TRUE;*/
	return FALSE;
	}
Exemplo n.º 8
0
struct sm_stat *
sm_unmon_1_svc(mon_id *arg, struct svc_req *req)
{
  static sm_stat res;
  HostInfo *hp;

  if (debug)
  {
    syslog(LOG_DEBUG, "un-monitor request for host %s", arg->mon_name);
    syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
      arg->my_id.my_name, arg->my_id.my_prog,
      arg->my_id.my_vers, arg->my_id.my_proc);
  }

  if ((hp = find_host(arg->mon_name, FALSE)))
  {
    if (do_unmon(hp, &arg->my_id)) sync_file();
    else
    {
      syslog(LOG_ERR, "unmon request from %s, no matching monitor",
	arg->my_id.my_name);
    }
  }
  else syslog(LOG_ERR, "unmon request from %s for unknown host %s",
    arg->my_id.my_name, arg->mon_name);

  res.state = status_info->ourState;

  return (&res);
}
Exemplo n.º 9
0
void *
sm_notify_1_svc(stat_chge *arg, struct svc_req *req)
{
  struct timeval timeout = { 20, 0 };	/* 20 secs timeout		*/
  CLIENT *cli;
  static char dummy; 
  sm_status tx_arg;		/* arg sent to callback procedure	*/
  MonList *lp;
  HostInfo *hp;
  pid_t pid;

  if (debug) syslog(LOG_DEBUG, "notify from host %s, new state %d",
    arg->mon_name, arg->state);

  hp = find_host(arg->mon_name, FALSE);
  if (!hp)
  {
    /* Never heard of this host - why is it notifying us?		*/
    syslog(LOG_ERR, "Unsolicited notification from host %s", arg->mon_name);
    return (&dummy);
  }
  lp = hp->monList;
  if (!lp) return (&dummy);	/* We know this host, but have no	*/
				/* outstanding requests.		*/
  pid = fork();
  if (pid == -1)
  {
    syslog(LOG_ERR, "Unable to fork notify process - %s", strerror(errno));
    return (NULL);
  }
  if (pid) return (&dummy);	/* Parent returns			*/

  while (lp)
  {
    tx_arg.mon_name = arg->mon_name;
    tx_arg.state = arg->state;
    memcpy(tx_arg.priv, lp->notifyData, sizeof(tx_arg.priv));
    cli = clnt_create(lp->notifyHost, lp->notifyProg, lp->notifyVers, "udp");
    if (!cli)
    {
      syslog(LOG_ERR, "Failed to contact host %s%s", lp->notifyHost,
        clnt_spcreateerror(""));
    }
    else
    {
      if (clnt_call(cli, lp->notifyProc, (xdrproc_t)xdr_sm_status, &tx_arg,
	  (xdrproc_t)xdr_void, &dummy, timeout) != RPC_SUCCESS)
      {
        syslog(LOG_ERR, "Failed to call rpc.statd client at host %s",
	  lp->notifyHost);
      }
      clnt_destroy(cli);
    }
    lp = lp->next;
  }

  exit (0);	/* Child quits	*/
}
Exemplo n.º 10
0
/* removes invalid and old downtime entries from the downtime file */
int xdddefault_validate_downtime_data(void) {
	scheduled_downtime *temp_downtime;
	scheduled_downtime *next_downtime;
	int update_file = FALSE;
	int save = TRUE;

	/* remove stale downtimes */
	for (temp_downtime = scheduled_downtime_list; temp_downtime != NULL; temp_downtime = next_downtime) {

		next_downtime = temp_downtime->next;
		save = TRUE;

		/* delete downtimes with invalid host names */
		if (find_host(temp_downtime->host_name) == NULL)
			save = FALSE;

		/* delete downtimes with invalid service descriptions */
		if (temp_downtime->type == SERVICE_DOWNTIME && find_service(temp_downtime->host_name, temp_downtime->service_description) == NULL)
			save = FALSE;

		/* delete downtimes that have expired */
		if (temp_downtime->end_time < time(NULL))
			save = FALSE;

		/* delete the downtime */
		if (save == FALSE) {
			update_file = TRUE;
			delete_downtime(temp_downtime->type, temp_downtime->downtime_id);
		}
	}

	/* remove triggered downtimes without valid parents */
	for (temp_downtime = scheduled_downtime_list; temp_downtime != NULL; temp_downtime = next_downtime) {

		next_downtime = temp_downtime->next;
		save = TRUE;

		if (temp_downtime->triggered_by == 0)
			continue;

		if (find_host_downtime(temp_downtime->triggered_by) == NULL && find_service_downtime(temp_downtime->triggered_by) == NULL)
			save = FALSE;

		/* delete the downtime */
		if (save == FALSE) {
			update_file = TRUE;
			delete_downtime(temp_downtime->type, temp_downtime->downtime_id);
		}
	}

	/* update downtime file */
	if (update_file == TRUE)
		xdddefault_save_downtime_data();

	return OK;
}
Exemplo n.º 11
0
static void process_status(json_t * payload) {
	char * host_name, *service_description = NULL, *output = NULL;
	check_result * newcr = NULL, t;

	init_check_result(&t);
	t.output_file = NULL;
	t.output_file_fp = NULL;
	if(get_values(payload,
		"host_name", JSON_STRING, 1, &host_name,
		"service_description", JSON_STRING, 0, &service_description,
		"output", JSON_STRING, 1, &output,
		"return_code", JSON_INTEGER, 1, &t.return_code,
		"start_time", JSON_TIMEVAL, 0, &t.start_time,
		"finish_time", JSON_TIMEVAL, 1, &t.finish_time,
		"check_type", JSON_INTEGER, 1, &t.check_type,
		"check_options", JSON_INTEGER, 0, &t.check_options,
		"scheduled_check", JSON_INTEGER, 0, &t.scheduled_check,
		"reschedule_check", JSON_INTEGER, 0, &t.reschedule_check,
		"latency", JSON_REAL, 0, &t.latency,
		"early_timeout", JSON_INTEGER, 0, &t.early_timeout,
		"exited_ok", JSON_INTEGER, 0, &t.exited_ok,
		NULL) != 0) {
		json_decref(payload);
		return;
	}

	service * service_target = NULL;
	if(service_description)
		service_target = find_service(host_name, service_description);
	host * host_target = find_host(host_name);
	if(host_target == NULL || (service_description && !service_target)) {
		json_decref(payload);
		return;
	}

	newcr = malloc(sizeof(check_result));
	memcpy(newcr, &t, sizeof(check_result));
	newcr->host_name = strdup(host_name);
	if(service_target) {
		newcr->service_description = strdup(service_description);
		newcr->object_check_type = SERVICE_CHECK;
	}
	newcr->output = strdup(output);
	json_decref(payload);
#ifdef HAVE_NAGIOS4
	newcr->engine = &nagmq_check_engine;
	process_check_result(newcr);
#else
#ifdef HAVE_ADD_CHECK_RESULT_ONE
	add_check_result_to_list(newcr);
#elif defined(HAVE_ADD_CHECK_RESULT_TWO)
	add_check_result_to_list(&check_result_list, newcr);
#endif
#endif
}
Exemplo n.º 12
0
/* check is the current user is authorized to issue commands relating to a particular service */
int is_authorized_for_service_commands(service *svc, authdata *authinfo) {
	host *temp_host;
	contact *temp_contact;

	if(svc == NULL)
		return FALSE;

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	/* the user is authorized if they have rights to the service */
	if(is_authorized_for_service(svc, authinfo) == TRUE) {

		/* find the host */
		temp_host = find_host(svc->host_name);
		if(temp_host == NULL)
			return FALSE;

		/* find the contact */
		temp_contact = find_contact(authinfo->username);

		/* reject if contact is not allowed to issue commands */
		if(temp_contact && temp_contact->can_submit_commands == FALSE)
			return FALSE;

		/* see if this user is a contact for the host */
		if(is_contact_for_host(temp_host, temp_contact) == TRUE)
			return TRUE;

		/* see if this user is an escalated contact for the host */
		if(is_escalated_contact_for_host(temp_host, temp_contact) == TRUE)
			return TRUE;

		/* this user is a contact for the service, so they have permission... */
		if(is_contact_for_service(svc, temp_contact) == TRUE)
			return TRUE;

		/* this user is an escalated contact for the service, so they have permission... */
		if(is_escalated_contact_for_service(svc, temp_contact) == TRUE)
			return TRUE;

		/* this user is not a contact for the host, so they must have been given explicit permissions to all service commands */
		if(authinfo->authorized_for_all_service_commands == TRUE)
			return TRUE;
		}

	return FALSE;
	}
Exemplo n.º 13
0
/* checks for flexible (non-fixed) host downtime that should start now */
int check_pending_flex_host_downtime(host *hst) {
	scheduled_downtime *temp_downtime = NULL;
	time_t current_time = 0L;
	unsigned long * new_downtime_id = NULL;


	log_debug_info(DEBUGL_FUNCTIONS, 0, "check_pending_flex_host_downtime()\n");

	if(hst == NULL)
		return ERROR;

	time(&current_time);

	/* if host is currently up, nothing to do */
	if(hst->current_state == HOST_UP)
		return OK;

	/* check all downtime entries */
	for(temp_downtime = scheduled_downtime_list; temp_downtime != NULL; temp_downtime = temp_downtime->next) {

		if(temp_downtime->type != HOST_DOWNTIME)
			continue;

		if(temp_downtime->fixed == TRUE)
			continue;

		if(temp_downtime->is_in_effect == TRUE)
			continue;

		/* triggered downtime entries should be ignored here */
		if(temp_downtime->triggered_by != 0)
			continue;

		/* this entry matches our host! */
		if(find_host(temp_downtime->host_name) == hst) {

			/* if the time boundaries are okay, start this scheduled downtime */
			if(temp_downtime->start_time <= current_time && current_time <= temp_downtime->end_time) {

				log_debug_info(DEBUGL_DOWNTIME, 0, "Flexible downtime (id=%lu) for host '%s' starting now...\n", temp_downtime->downtime_id, hst->name);
				temp_downtime->flex_downtime_start = current_time;
				if((new_downtime_id = (unsigned long *)malloc(sizeof(unsigned long *)))) {
					*new_downtime_id = temp_downtime->downtime_id;
					temp_downtime->start_event = schedule_new_event(EVENT_SCHEDULED_DOWNTIME, TRUE, temp_downtime->flex_downtime_start, FALSE, 0, NULL, FALSE, (void *)new_downtime_id, NULL, 0);
					}
				}
			}
		}

	return OK;
	}
Exemplo n.º 14
0
struct sm_stat_res *
sm_mon_1_svc(mon *arg, struct svc_req *req)
{
  static sm_stat_res res;
  HostInfo *hp;
  MonList *lp;

  if (debug)
  {
    syslog(LOG_DEBUG, "monitor request for host %s", arg->mon_id.mon_name);
    syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
      arg->mon_id.my_id.my_name,
      arg->mon_id.my_id.my_prog, arg->mon_id.my_id.my_vers,
      arg->mon_id.my_id.my_proc);
  }

  res.res_stat = stat_fail;	/* Assume fail until set otherwise	*/
  res.state = status_info->ourState;

  /* Find existing host entry, or create one if not found		*/
  /* If find_host() fails, it will have logged the error already.	*/
  if (!gethostbyname(arg->mon_id.mon_name))
  {
    syslog(LOG_ERR, "Invalid hostname to sm_mon: %s", arg->mon_id.mon_name);
  }
  else if ((hp = find_host(arg->mon_id.mon_name, TRUE)))
  {
    lp = (MonList *)malloc(sizeof(MonList));
    if (!lp)
    {
      syslog(LOG_ERR, "Out of memory");
    }
    else
    {
      strncpy(lp->notifyHost, arg->mon_id.my_id.my_name, SM_MAXSTRLEN);
      lp->notifyProg = arg->mon_id.my_id.my_prog;
      lp->notifyVers = arg->mon_id.my_id.my_vers;
      lp->notifyProc = arg->mon_id.my_id.my_proc;
      memcpy(lp->notifyData, arg->priv, sizeof(lp->notifyData));

      lp->next = hp->monList;
      hp->monList = lp;
      sync_file();

      res.res_stat = stat_succ;	/* Report success			*/
    }
  }

  return (&res);
}
Exemplo n.º 15
0
void
make_connection(struct socket *socket, struct uri *uri,
		socket_connect_T connect_done, int no_cache)
{
	unsigned char *host = get_uri_string(uri, URI_DNS_HOST);
	struct connect_info *connect_info;
	enum dns_result result;
	enum blacklist_flags verify;

	socket->ops->set_timeout(socket, connection_state(0));

	if (!host) {
		socket->ops->retry(socket, connection_state(S_OUT_OF_MEM));
		return;
	}

	connect_info = init_connection_info(uri, socket, connect_done);
	if (!connect_info) {
		mem_free(host);
		socket->ops->retry(socket, connection_state(S_OUT_OF_MEM));
		return;
	}

	socket->connect_info = connect_info;
	/* XXX: Keep here and not in init_connection_info() to make
	 * complete_connect_socket() work from the HTTP implementation. */
	socket->need_ssl = get_protocol_need_ssl(uri->protocol);
	if (!socket->set_no_tls) {
		enum blacklist_flags flags = get_blacklist_flags(uri);
		socket->no_tls = ((flags & SERVER_BLACKLIST_NO_TLS) != 0);
		socket->set_no_tls = 1;
	}

	verify = get_blacklist_flags(uri);
	socket->verify = ((verify & SERVER_BLACKLIST_NO_CERT_VERIFY) == 0);

	debug_transfer_log("\nCONNECTION: ", -1);
	debug_transfer_log(host, -1);
	debug_transfer_log("\n", -1);

	result = find_host(host, &connect_info->dnsquery, (dns_callback_T) dns_found,
			   socket, no_cache);

	mem_free(host);

	if (result == DNS_ASYNC)
		socket->ops->set_state(socket, connection_state(S_DNS));
}
Exemplo n.º 16
0
/* check is the current user is authorized to issue commands relating to a particular hostgroup */
int is_authorized_for_hostgroup_commands(hostgroup *hg, authdata *authinfo) {
	hostsmember *temp_hostsmember;
	host *temp_host;

	if(hg == NULL)
		return FALSE;

	/* see if user is authorized for all hosts in the hostgroup */
	for(temp_hostsmember = hg->members; temp_hostsmember != NULL; temp_hostsmember = temp_hostsmember->next) {
		temp_host = find_host(temp_hostsmember->host_name);
		if(is_authorized_for_host_commands(temp_host, authinfo) == FALSE)
			return FALSE;
		}

	return TRUE;
	}
Exemplo n.º 17
0
Arquivo: workers.c Projeto: atj/nagios
static int handle_worker_check(wproc_result *wpres, worker_process *wp, worker_job *job)
{
	int result = ERROR;
	check_result *cr = (check_result *)job->arg;

	memcpy(&cr->rusage, &wpres->rusage, sizeof(wpres->rusage));
	cr->start_time.tv_sec = wpres->start.tv_sec;
	cr->start_time.tv_usec = wpres->start.tv_usec;
	cr->finish_time.tv_sec = wpres->stop.tv_sec;
	cr->finish_time.tv_usec = wpres->stop.tv_usec;
	if (WIFEXITED(wpres->wait_status)) {
		cr->return_code = WEXITSTATUS(wpres->wait_status);
	} else {
		cr->return_code = STATE_UNKNOWN;
	}

	if (wpres->outstd) {
		cr->output = strdup(wpres->outstd);
	} else if (wpres->outerr) {
		asprintf(&cr->output, "(No output on stdout) stderr: %s", wpres->outerr);
	} else {
		cr->output = NULL;
	}

	cr->early_timeout = wpres->early_timeout;
	cr->exited_ok = wpres->exited_ok;

	if (cr->service_description) {
		service *svc = find_service(cr->host_name, cr->service_description);
		if (svc)
			result = handle_async_service_check_result(svc, cr);
		else
			logit(NSLOG_RUNTIME_WARNING, TRUE, "Worker: Failed to locate service '%s' on host '%s'. Result from job %d (%s) will be dropped.\n",
				  cr->service_description, cr->host_name, job->id, job->command);
	} else {
		host *hst = find_host(cr->host_name);
		if (hst)
			result = handle_async_host_check_result_3x(hst, cr);
		else
			logit(NSLOG_RUNTIME_WARNING, TRUE, "Worker: Failed to locate host '%s'. Result from job %d (%s) will be dropped.\n",
				  cr->host_name, job->id, job->command);
	}
	free_check_result(cr);

	return result;
}
Exemplo n.º 18
0
static void process_acknowledgement(json_t * payload) {
	char *host_name, *service_description = NULL,
		*author_name, *comment_data;
	int persistent_comment = 0, notify_contacts = 0,
		acknowledgement_type = 0, end_time = 0;
	host * host_target = NULL;
	service * service_target = NULL;
	json_error_t err;
	if(get_values(payload,
		"host_name", JSON_STRING, 1, &host_name,
		"service_description", JSON_STRING, 0, &service_description,
		"author_name", JSON_STRING, 1, &author_name,
		"comment_data", JSON_STRING, 1, &comment_data,
		"acknowledgement_type", JSON_INTEGER, 0, &acknowledgement_type,
		"notify_contacts", JSON_TRUE, 0, &notify_contacts,
		"persistent_comment", JSON_TRUE, 0, &persistent_comment,
#ifdef HAVE_ICINGA
		"end_time", JSON_INTEGER, 0, &end_time,
#endif
		NULL) != 0) {
		json_decref(payload);
		return;
	}

	host_target = find_host(host_name);
	if(service_description)
		service_target = find_service(host_name, service_description);

#ifdef HAVE_ICINGA
	if(service_target)
		acknowledge_service_problem(service_target, author_name, comment_data,
			acknowledgement_type, notify_contacts, persistent_comment, end_time);
	else if(host_target)
		acknowledge_host_problem(host_target, author_name, comment_data,
			acknowledgement_type, notify_contacts, persistent_comment, end_time);
#else
	if(service_target)
		acknowledge_service_problem(service_target, author_name, comment_data,
			acknowledgement_type, notify_contacts, persistent_comment);
	else if(host_target)
		acknowledge_host_problem(host_target, author_name, comment_data,
			acknowledgement_type, notify_contacts, persistent_comment);
#endif
	json_decref(payload);
}
Exemplo n.º 19
0
string create_unknown_node(const string& prev_node) 
{
    static HostClass* prev_res = 0;

    if(prev_res && prev_res->ip == prev_node) {
	
	prev_res->unknown_hosts++;
	return prev_node;
    }
    
    HostClass hu;
    hostlist.push_back(hu);

    prev_res = find_host(hostlist, hu.ip);
    if(!prev_res)
	cout << "BIGMISTAKE" << endl;

    return hu.name();
}
Exemplo n.º 20
0
hostescalation *add_hostescalation(char *host_name, int first_notification, int last_notification, double notification_interval, char *escalation_period, int escalation_options)
{
	hostescalation *new_hostescalation = NULL;
	host *h;
	timeperiod *tp = NULL;

	/* make sure we have the data we need */
	if (host_name == NULL || !*host_name) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Host escalation host name is NULL\n");
		return NULL;
	}
	if (!(h = find_host(host_name))) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Host '%s' has an escalation, but is not defined anywhere!\n", host_name);
		return NULL;
	}
	if (escalation_period && !(tp = find_timeperiod(escalation_period))) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Unable to locate timeperiod '%s' for hostescalation '%s'\n",
		       escalation_period, host_name);
		return NULL;
	}

	new_hostescalation = nm_calloc(1, sizeof(*new_hostescalation));

	/* add the escalation to its host */
	if (prepend_object_to_objectlist(&h->escalation_list, new_hostescalation) != OK) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Could not add hostescalation to host '%s'\n", host_name);
		free(new_hostescalation);
		return NULL;
	}

	/* assign vars. Object names are immutable, so no need to copy */
	new_hostescalation->host_name = h->name;
	new_hostescalation->host_ptr = h;
	new_hostescalation->escalation_period = tp ? tp->name : NULL;
	new_hostescalation->escalation_period_ptr = tp;
	new_hostescalation->first_notification = first_notification;
	new_hostescalation->last_notification = last_notification;
	new_hostescalation->notification_interval = (notification_interval <= 0) ? 0 : notification_interval;
	new_hostescalation->escalation_options = escalation_options;

	new_hostescalation->id = num_objects.hostescalations++;
	return new_hostescalation;
}
Exemplo n.º 21
0
/* updates service performance data */
int xpddefault_update_service_performance_data(service *svc)
{
	nagios_macros mac;
	host *hst;

	/*
	 * bail early if we've got nothing to do so we don't spend a lot
	 * of time calculating macros that never get used
	 * on distributed setups, empty perfdata results are required, so
	 * only drop out if demanded via configs.
	*/
	if (service_perfdata_process_empty_results == FALSE) {
		if (!svc || !svc->perf_data || !*svc->perf_data) {
			return OK;
		}
		if ((!service_perfdata_fp || !service_perfdata_file_template) && !service_perfdata_command) {
			return OK;
		}

	}
	/*
	 * we know we've got some work to do, so grab the necessary
	 * macros and get busy
	 */
	memset(&mac, 0, sizeof(mac));
	hst = find_host(svc->host_name);
	grab_host_macros_r(&mac, hst);
	grab_service_macros_r(&mac, svc);

	/* run the performance data command */
	xpddefault_run_service_performance_data_command(&mac, svc);

	/* get rid of used memory we won't need anymore */
	clear_argv_macros_r(&mac);

	/* update the performance data file */
	xpddefault_update_service_performance_data_file(&mac, svc);

	/* now free() it all */
	clear_volatile_macros_r(&mac);

	return OK;
}
Exemplo n.º 22
0
/* check if user is authorized to view information about a particular service */
int is_authorized_for_service(service *svc, authdata *authinfo) {
	host *temp_host;
	contact *temp_contact;

	if(svc == NULL)
		return FALSE;

	/* if we're not using authentication, fake it */
	if(use_authentication == FALSE)
		return TRUE;

	/* if this user has not authenticated return error */
	if(authinfo->authenticated == FALSE)
		return FALSE;

	/* if this user is authorized for all services, they are for this one... */
	if(is_authorized_for_all_services(authinfo) == TRUE)
		return TRUE;

	/* find the host */
	temp_host = find_host(svc->host_name);
	if(temp_host == NULL)
		return FALSE;

	/* if this user is authorized for this host, they are for all services on it as well... */
	if(is_authorized_for_host(temp_host, authinfo) == TRUE)
		return TRUE;

	/* find the contact */
	temp_contact = find_contact(authinfo->username);

	/* see if this user is a contact for the service */
	if(is_contact_for_service(svc, temp_contact) == TRUE)
		return TRUE;

	/* see if this user is an escalated contact for the service */
	if(is_escalated_contact_for_service(svc, temp_contact) == TRUE)
		return TRUE;

	return FALSE;
	}
Exemplo n.º 23
0
int register_host(host *new_host)
{

	g_return_val_if_fail(host_hash_table != NULL, ERROR);

	if ((find_host(new_host->name))) {
		nm_log(NSLOG_CONFIG_ERROR, "Error: Host '%s' has already been defined\n", new_host->name);
		return ERROR;
	}

	g_hash_table_insert(host_hash_table, new_host->name, new_host);

	new_host->id = num_objects.hosts++;
	host_ary[new_host->id] = new_host;
	if (new_host->id)
		host_ary[new_host->id - 1]->next = new_host;
	else
		host_list = new_host;

	return OK;
}
Exemplo n.º 24
0
/* updates service performance data */
int xpddefault_update_service_performance_data(service *svc)
{
	nagios_macros mac;
	host *hst;

	/*
	 * bail early if we've got nothing to do so we don't spend a lot
	 * of time calculating macros that never get used
	 */
	if (!svc || !svc->perf_data || !*svc->perf_data) {
		return OK;
	}
	if ((!xpddefault_service_perfdata_fp || !xpddefault_service_perfdata_file_template) && !xpddefault_service_perfdata_command)
	{
		return OK;
	}

	/*
	 * we know we've got some work to do, so grab the necessary
	 * macros and get busy
	 */
	memset(&mac, 0, sizeof(mac));
	hst = find_host(svc->host_name);
	grab_host_macros(&mac, hst);
	grab_service_macros(&mac, svc);

	/* run the performance data command */
	xpddefault_run_service_performance_data_command(&mac, svc);

	/* get rid of used memory we won't need anymore */
	clear_argv_macros(&mac);

	/* update the performance data file */
	xpddefault_update_service_performance_data_file(&mac, svc);

	/* now free() it all */
	clear_volatile_macros(&mac);

	return OK;
}
Exemplo n.º 25
0
int		c_create(char *host, int port)
{
	struct protoent		*protocol;
	struct sockaddr_in	sin;
	int					sock;
	char				*ip;

	if ((ip = find_host(host)) == NULL)
		ip = ft_strdup(host);
	if (!(protocol = getprotobyname("tcp")))
		return (-1);
	sock = socket(PF_INET, SOCK_STREAM, protocol->p_proto);
	sin.sin_family = AF_INET;
	sin.sin_port = htons(port);
	sin.sin_addr.s_addr = inet_addr(ip);
	if ((connect(sock, (struct sockaddr *)&sin, sizeof(sin))) == -1)
	{
		ft_putendl("Unable to connect to server");
		return (-1);
	}
	return (sock);
}
Exemplo n.º 26
0
/* displays hostgroup status overview */
void display_hostgroup_overview(void) {
	hostgroup *temp_hostgroup;
	hostsmember *temp_member;
	host *temp_host;
	hoststatus *temp_hoststatus;


	/**** MAIN SCREEN (CARD 1) ****/
	printf("<card id='card1' title='Status Overview'>\n");
	printf("<p align='center' mode='nowrap'>\n");

	printf("<b><anchor title='Status Overview'>Status Overview<go href='%s' method='post'><postfield name='hostgroup' value='%s'/><postfield name='style' value='summary'/></go></anchor></b><br/><br/>\n", STATUSWML_CGI, escape_string(hostgroup_name));

	/* check all hostgroups */
	for(temp_hostgroup = hostgroup_list; temp_hostgroup != NULL; temp_hostgroup = temp_hostgroup->next) {

		if(show_all_hostgroups == FALSE && strcmp(temp_hostgroup->group_name, hostgroup_name))
			continue;

		if(is_authorized_for_hostgroup(temp_hostgroup, &current_authdata) == FALSE)
			continue;

		printf("<b>%s</b>\n", temp_hostgroup->alias);

		printf("<table columns='2' align='LL'>\n");

		/* check all hosts in this hostgroup */
		for(temp_member = temp_hostgroup->members; temp_member != NULL; temp_member = temp_member->next) {

			temp_host = find_host(temp_member->host_name);
			if(temp_host == NULL)
				continue;

			if(is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE)
				continue;

			temp_hoststatus = find_hoststatus(temp_host->name);
			if(temp_hoststatus == NULL)
				continue;

			printf("<tr><td><anchor title='%s'>", temp_host->name);
			if(temp_hoststatus->status == HOST_UP)
				printf("UP");
			else if(temp_hoststatus->status == HOST_PENDING)
				printf("PND");
			else if(temp_hoststatus->status == HOST_DOWN)
				printf("DWN");
			else if(temp_hoststatus->status == HOST_UNREACHABLE)
				printf("UNR");
			else
				printf("???");
			printf("<go href='%s' method='post'><postfield name='host' value='%s'/></go></anchor></td>", STATUSWML_CGI, temp_host->name);
			printf("<td>%s</td></tr>\n", temp_host->name);
			}

		printf("</table>\n");

		printf("<br/>\n");
		}

	if(show_all_hostgroups == FALSE)
		printf("<b><anchor title='View All Hostgroups'>View All Hostgroups<go href='%s' method='post'><postfield name='hostgroup' value='all'/><postfield name='style' value='overview'/></go></anchor></b>\n", STATUSWML_CGI);

	printf("</p>\n");
	printf("</card>\n");

	return;
	}
Exemplo n.º 27
0
void display_notifications(void) {
	char *temp_buffer;
	char *error_text = NULL;
	char date_time[MAX_DATETIME_LENGTH];
	char alert_level[MAX_INPUT_BUFFER];
	char alert_level_class[MAX_INPUT_BUFFER];
	char contact_name[MAX_INPUT_BUFFER];
	char service_name[MAX_INPUT_BUFFER];
	char host_name[MAX_INPUT_BUFFER];
	char method_name[MAX_INPUT_BUFFER];
	char displayed_host_name[MAX_INPUT_BUFFER];
	char displayed_service_desc[MAX_INPUT_BUFFER];
	int show_entry;
	int total_notifications = 0;
	int displayed_entries = 0;
	int notification_detail_type = NOTIFICATION_SERVICE_CRITICAL;
	int status = READLOG_OK;
	int odd = 0;
	int json_start = TRUE;
	host *temp_host = NULL;
	service *temp_service = NULL;
	hostgroup *temp_hostgroup = NULL;
	servicegroup *temp_servicegroup = NULL;
	logentry *temp_entry = NULL;
	logentry *entry_list = NULL;
	logfilter *filter_list = NULL;

	if (query_type == DISPLAY_HOSTGROUPS) {

		temp_hostgroup = find_hostgroup(query_hostgroup_name);

		if (temp_hostgroup == NULL) {
			print_generic_error_message("There are no host groups with this name defined.", NULL, 0);
			return;
		}
		/* make sure the user is authorized to view this hostgroup */
		if (show_partial_hostgroups == FALSE && is_authorized_for_hostgroup(temp_hostgroup, &current_authdata) == FALSE) {
			print_generic_error_message("It appears as though you do not have permission to view information for the host group you requested...", "If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI and check the authorization options in your CGI configuration file.", 0);
			return;
		}
	}

	if (query_type == DISPLAY_SERVICEGROUPS) {

		temp_servicegroup = find_servicegroup(query_servicegroup_name);

		if (temp_servicegroup == NULL) {
			print_generic_error_message("There are no service groups with this name defined.", NULL, 0);
			return;
		}
		/* make sure the user is authorized to view this servicegroup */
		if (show_partial_servicegroups == FALSE && is_authorized_for_servicegroup(temp_servicegroup, &current_authdata) == FALSE) {
			print_generic_error_message("It appears as though you do not have permission to view information for the service group you requested...", "If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI and check the authorization options in your CGI configuration file.", 0);
			return;
		}
	}

	add_log_filter(&filter_list, LOGENTRY_HOST_NOTIFICATION, LOGFILTER_INCLUDE);
	add_log_filter(&filter_list, LOGENTRY_SERVICE_NOTIFICATION, LOGFILTER_INCLUDE);

	/* scan the log file for notification data */
	status = get_log_entries(&entry_list, &filter_list, &error_text, NULL, reverse, ts_start, ts_end);

	free_log_filters(&filter_list);

	/* dealing with errors */
	if (status == READLOG_ERROR_WARNING) {
		if (error_text != NULL) {
			print_generic_error_message(error_text, NULL, 0);
			my_free(error_text);
		} else
			print_generic_error_message("Unkown error!", NULL, 0);
	}

	if (status == READLOG_ERROR_MEMORY)
			print_generic_error_message("Out of memory...", "showing all I could get!", 0);

	if (status == READLOG_ERROR_FATAL) {
		if (error_text != NULL) {
			print_generic_error_message(error_text, NULL, 0);
			my_free(error_text);
		}

		return;

	/* now we start displaying the notification entries */
	} else {
		if (content_type == JSON_CONTENT) {
			if (status != READLOG_OK)
				printf(",\n");
			printf("\"notifications\": [\n");
		} else if (content_type == CSV_CONTENT) {
			printf("%sHOST%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter);
			printf("%sSERVICE%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter);
			printf("%sTYPE%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter);
			printf("%sTIME%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter);
			printf("%sCONTACT%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter);
			printf("%sNOTIFICATION_COMMAND%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter);
			printf("%sINFORMATION%s\n", csv_data_enclosure, csv_data_enclosure);
		} else {
			printf("<table border='0' class='notifications' align='center'>\n");

			/* add export to csv, json, link */
			printf("<tr><td colspan='7'>");
			printf("<table width='100%%' cellspacing='0' cellpadding='0'><tr><td width='33%%'></td><td width='33%%' nowrap>");
			printf("<div class='page_selector'>\n");
			printf("<div id='page_navigation_copy'></div>");
			page_limit_selector(result_start);
			printf("</div>\n");
			printf("</td><td width='33%%' align='right' style='padding-right:2px'>\n");
			printf("<div class='csv_export_link'>");
			print_export_link(CSV_CONTENT, NOTIFICATIONS_CGI, NULL);
			print_export_link(JSON_CONTENT, NOTIFICATIONS_CGI, NULL);
			print_export_link(HTML_CONTENT, NOTIFICATIONS_CGI, NULL);
			printf("</div></td></tr></table>");

			printf("<tr>\n");
			printf("<th class='notifications'>Host</th>\n");
			printf("<th class='notifications'>Service</th>\n");
			printf("<th class='notifications'>Type</th>\n");
			printf("<th class='notifications'>Time</th>\n");
			printf("<th class='notifications'>Contact</th>\n");
			printf("<th class='notifications'>Notification Command</th>\n");
			printf("<th class='notifications'>Information</th>\n");
			printf("</tr>\n");
		}

		/* check all entries */
		for (temp_entry = entry_list; temp_entry != NULL; temp_entry = temp_entry->next) {

			/* get the date/time */
			get_time_string(&temp_entry->timestamp, date_time, (int)sizeof(date_time), SHORT_DATE_TIME);
			strip(date_time);

			/* get the contact name */
			temp_buffer = (char *)strtok(temp_entry->entry_text, ":");
			temp_buffer = (char *)strtok(NULL, ";");
			snprintf(contact_name, sizeof(contact_name), "%s", (temp_buffer == NULL) ? "" : temp_buffer + 1);
			contact_name[sizeof(contact_name) - 1] = '\x0';

			/* get the host name */
			temp_buffer = (char *)strtok(NULL, ";");
			snprintf(host_name, sizeof(host_name), "%s", (temp_buffer == NULL) ? "" : temp_buffer);
			host_name[sizeof(host_name) - 1] = '\x0';

			/* get the service name */
			service_name[0] = '\x0';
			if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) {
				temp_buffer = (char *)strtok(NULL, ";");
				snprintf(service_name, sizeof(service_name), "%s", (temp_buffer == NULL) ? "" : temp_buffer);
				service_name[sizeof(service_name) - 1] = '\x0';
			}

			/* get the alert level */
			temp_buffer = (char *)strtok(NULL, ";");
			snprintf(alert_level, sizeof(alert_level), "%s", (temp_buffer == NULL) ? "" : temp_buffer);
			alert_level[sizeof(alert_level) - 1] = '\x0';

			if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) {

				if (!strcmp(alert_level, "CRITICAL")) {
					notification_detail_type = NOTIFICATION_SERVICE_CRITICAL;
					strcpy(alert_level_class, "CRITICAL");
				} else if (!strcmp(alert_level, "WARNING")) {
					notification_detail_type = NOTIFICATION_SERVICE_WARNING;
					strcpy(alert_level_class, "WARNING");
				} else if (!strcmp(alert_level, "RECOVERY") || !strcmp(alert_level, "OK")) {
					strcpy(alert_level, "OK");
					notification_detail_type = NOTIFICATION_SERVICE_RECOVERY;
					strcpy(alert_level_class, "OK");
				} else if (strstr(alert_level, "CUSTOM (")) {
					notification_detail_type = NOTIFICATION_SERVICE_CUSTOM;
					strcpy(alert_level_class, "CUSTOM");
				} else if (strstr(alert_level, "ACKNOWLEDGEMENT (")) {
					notification_detail_type = NOTIFICATION_SERVICE_ACK;
					strcpy(alert_level_class, "ACKNOWLEDGEMENT");
				} else if (strstr(alert_level, "FLAPPINGSTART (")) {
					strcpy(alert_level, "FLAPPING START");
					notification_detail_type = NOTIFICATION_SERVICE_FLAP;
					strcpy(alert_level_class, "UNKNOWN");
				} else if (strstr(alert_level, "FLAPPINGSTOP (")) {
					strcpy(alert_level, "FLAPPING STOP");
					notification_detail_type = NOTIFICATION_SERVICE_FLAP;
					strcpy(alert_level_class, "UNKNOWN");
				} else {
					strcpy(alert_level, "UNKNOWN");
					notification_detail_type = NOTIFICATION_SERVICE_UNKNOWN;
					strcpy(alert_level_class, "UNKNOWN");
				}
			} else {

				if (!strcmp(alert_level, "DOWN")) {
					strncpy(alert_level, "HOST DOWN", sizeof(alert_level));
					strcpy(alert_level_class, "HOSTDOWN");
					notification_detail_type = NOTIFICATION_HOST_DOWN;
				} else if (!strcmp(alert_level, "UNREACHABLE")) {
					strncpy(alert_level, "HOST UNREACHABLE", sizeof(alert_level));
					strcpy(alert_level_class, "HOSTUNREACHABLE");
					notification_detail_type = NOTIFICATION_HOST_UNREACHABLE;
				} else if (!strcmp(alert_level, "RECOVERY") || !strcmp(alert_level, "UP")) {
					strncpy(alert_level, "HOST UP", sizeof(alert_level));
					strcpy(alert_level_class, "HOSTUP");
					notification_detail_type = NOTIFICATION_HOST_RECOVERY;
				} else if (strstr(alert_level, "CUSTOM (")) {
					strcpy(alert_level_class, "HOSTCUSTOM");
					notification_detail_type = NOTIFICATION_HOST_CUSTOM;
				} else if (strstr(alert_level, "ACKNOWLEDGEMENT (")) {
					strcpy(alert_level_class, "HOSTACKNOWLEDGEMENT");
					notification_detail_type = NOTIFICATION_HOST_ACK;
				} else if (strstr(alert_level, "FLAPPINGSTART (")) {
					strcpy(alert_level, "FLAPPING START");
					strcpy(alert_level_class, "UNKNOWN");
					notification_detail_type = NOTIFICATION_HOST_FLAP;
				} else if (strstr(alert_level, "FLAPPINGSTOP (")) {
					strcpy(alert_level, "FLAPPING STOP");
					strcpy(alert_level_class, "UNKNOWN");
					notification_detail_type = NOTIFICATION_HOST_FLAP;
				}
			}

			/* get the method name */
			temp_buffer = (char *)strtok(NULL, ";");
			snprintf(method_name, sizeof(method_name), "%s", (temp_buffer == NULL) ? "" : temp_buffer);
			method_name[sizeof(method_name) - 1] = '\x0';

			/* move to the informational message */
			temp_buffer = strtok(NULL, ";");

			show_entry = FALSE;

			/* if we're searching by contact, filter out unwanted contact */
			if (query_type == DISPLAY_CONTACTS) {
				if (find_all == TRUE)
					show_entry = TRUE;
				else if (!strcmp(query_contact_name, contact_name))
					show_entry = TRUE;
			}

			/* search host */
			else if (query_type == DISPLAY_HOSTS) {
				if (find_all == TRUE)
					show_entry = TRUE;
				else if (!strcmp(query_host_name, host_name))
					show_entry = TRUE;
			}

			/* searching service */
			else if (query_type == DISPLAY_SERVICES) {
				if (!strcmp(query_host_name, host_name) && !strcmp(query_svc_description, service_name))
					show_entry = TRUE;
			}

			/* Set TRUE here, get's checked later on */
			else if (query_type == DISPLAY_HOSTGROUPS || query_type == DISPLAY_SERVICEGROUPS) {
				show_entry = TRUE;
			}

			if (show_entry == TRUE) {
				if (notification_options == NOTIFICATION_ALL)
					show_entry = TRUE;
				else if (notification_options == NOTIFICATION_HOST_ALL && temp_entry->type == LOGENTRY_HOST_NOTIFICATION)
					show_entry = TRUE;
				else if (notification_options == NOTIFICATION_SERVICE_ALL && temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION)
					show_entry = TRUE;
				else if (notification_detail_type & notification_options)
					show_entry = TRUE;
				else
					show_entry = FALSE;
			}

			/* make sure user has authorization to view this notification */
			temp_host = find_host(host_name);
			if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION)
				temp_service = find_service(host_name, service_name);

			if (temp_host != NULL) {
				snprintf(displayed_host_name, sizeof(displayed_host_name), "%s", (temp_host->display_name != NULL && content_type == HTML_CONTENT) ? temp_host->display_name : temp_host->name);
				displayed_host_name[sizeof(displayed_host_name) - 1] = '\x0';

				if (temp_entry->type == LOGENTRY_HOST_NOTIFICATION) {
					if (is_authorized_for_host(temp_host, &current_authdata) == FALSE)
						show_entry = FALSE;
					else if (query_type == DISPLAY_HOSTGROUPS && is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE)
						show_entry = FALSE;
					else if (query_type == DISPLAY_SERVICEGROUPS && is_host_member_of_servicegroup(temp_servicegroup, temp_host) == FALSE)
						show_entry = FALSE;
				} else {
					if (temp_service != NULL) {
						snprintf(displayed_service_desc, sizeof(displayed_service_desc), "%s", (temp_service->display_name != NULL && content_type == HTML_CONTENT) ? temp_service->display_name : temp_service->description);
						displayed_service_desc[sizeof(displayed_service_desc) - 1] = '\x0';

						if (is_authorized_for_service(temp_service, &current_authdata) == FALSE)
							show_entry = FALSE;
						else if (query_type == DISPLAY_HOSTGROUPS && is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE)
							show_entry = FALSE;
						else if (query_type == DISPLAY_SERVICEGROUPS && is_service_member_of_servicegroup(temp_servicegroup, temp_service) == FALSE)
							show_entry = FALSE;
					} else {
						if (is_authorized_for_all_services(&current_authdata) == FALSE)
							show_entry = FALSE;

						snprintf(displayed_service_desc, sizeof(displayed_service_desc), "%s", service_name);
						displayed_service_desc[sizeof(displayed_service_desc) - 1] = '\x0';
					}
				}
			} else {
				if (temp_entry->type == LOGENTRY_HOST_NOTIFICATION) {
					if (is_authorized_for_all_hosts(&current_authdata) == FALSE)
						show_entry = FALSE;
				} else {
					if (is_authorized_for_all_services(&current_authdata) == FALSE)
						show_entry = FALSE;

					snprintf(displayed_service_desc, sizeof(displayed_service_desc), "%s", service_name);
					displayed_service_desc[sizeof(displayed_service_desc) - 1] = '\x0';
				}

				snprintf(displayed_host_name, sizeof(displayed_host_name), "%s", host_name);
				displayed_host_name[sizeof(displayed_host_name) - 1] = '\x0';
			}

			if (show_entry == TRUE) {

				if (result_limit != 0  && (((total_notifications + 1) < result_start) || (total_notifications >= ((result_start + result_limit) - 1)))) {
					total_notifications++;
					continue;
				}

				displayed_entries++;
				total_notifications++;

				if (odd)
					odd = 0;
				else
					odd = 1;

				if (content_type == JSON_CONTENT) {
					if (json_start == FALSE)
						printf(",\n");
					printf("{\"host_name\": \"%s\", ", json_encode(temp_host->name));
					printf("\"host_display_name\": \"%s\", ", (temp_host->display_name != NULL) ? json_encode(temp_host->display_name) : json_encode(temp_host->name));
					if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) {
						printf("\"service_description\": \"%s\", ", json_encode(temp_service->description));
						printf("\"service_display_name\": \"%s\", ", (temp_service->display_name != NULL) ? json_encode(temp_service->display_name) : json_encode(temp_service->description));
					} else {
						printf("\"service_description\": null, ");
						printf("\"service_display_name\": null, ");
					}
					printf("\"type\": \"%s\", ", alert_level);
					printf("\"time\": \"%s\", ", date_time);
					printf("\"contact\": \"%s\", ", json_encode(contact_name));
					printf("\"notification_command\": \"%s\", ", json_encode(method_name));
					printf("\"information\": \"%s\"}", json_encode(escape_newlines(temp_buffer)));
				} else if (content_type == CSV_CONTENT) {
					printf("%s%s%s%s", csv_data_enclosure, displayed_host_name, csv_data_enclosure, csv_delimiter);
					if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION)
						printf("%s%s%s%s", csv_data_enclosure, displayed_service_desc, csv_data_enclosure, csv_delimiter);
					else
						printf("%sN/A%s%s", csv_data_enclosure, csv_data_enclosure, csv_delimiter);
					printf("%s%s%s%s", csv_data_enclosure, alert_level, csv_data_enclosure, csv_delimiter);
					printf("%s%s%s%s", csv_data_enclosure, date_time, csv_data_enclosure, csv_delimiter);
					printf("%s%s%s%s", csv_data_enclosure, contact_name, csv_data_enclosure, csv_delimiter);
					printf("%s%s%s%s", csv_data_enclosure, method_name, csv_data_enclosure, csv_delimiter);
					printf("%s%s%s\n", csv_data_enclosure, escape_newlines(temp_buffer), csv_data_enclosure);
				} else {
					printf("<tr class='notifications%s'>\n", (odd) ? "Even" : "Odd");
					if (temp_host != NULL)
						printf("<td class='notifications%s'><a href='%s?type=%d&amp;host=%s'>%s</a></td>\n", (odd) ? "Even" : "Odd", EXTINFO_CGI, DISPLAY_HOST_INFO, url_encode(host_name), displayed_host_name);
					else
						printf("<td class='notifications%s'>%s</td>\n", (odd) ? "Even" : "Odd", displayed_host_name);
					if (temp_entry->type == LOGENTRY_SERVICE_NOTIFICATION) {
						if (temp_service != NULL) {
							printf("<td class='notifications%s'><a href='%s?type=%d&amp;host=%s", (odd) ? "Even" : "Odd", EXTINFO_CGI, DISPLAY_SERVICE_INFO, url_encode(host_name));
							printf("&amp;service=%s'>%s</a></td>\n", url_encode(service_name), displayed_service_desc);
						} else
							printf("<td class='notifications%s'>%s</td>\n", (odd) ? "Even" : "Odd", displayed_service_desc);

					} else
						printf("<td class='notifications%s'>N/A</td>\n", (odd) ? "Even" : "Odd");
					printf("<td class='notifications%s'>%s</td>\n", alert_level_class, alert_level);
					printf("<td class='notifications%s'>%s</td>\n", (odd) ? "Even" : "Odd", date_time);
					printf("<td class='notifications%s'><a href='%s?type=contacts#%s'>%s</a></td>\n", (odd) ? "Even" : "Odd", CONFIG_CGI, url_encode(contact_name), contact_name);
					printf("<td class='notifications%s'><a href='%s?type=commands#%s'>%s</a></td>\n", (odd) ? "Even" : "Odd", CONFIG_CGI, url_encode(method_name), method_name);
					printf("<td class='notifications%s'>%s</td>\n", (odd) ? "Even" : "Odd", html_encode(temp_buffer, FALSE));
					printf("</tr>\n");
				}
				if (json_start == TRUE)
					json_start = FALSE;
			}
		}
	}

	free_log_entries(&entry_list);

	if (content_type != CSV_CONTENT && content_type != JSON_CONTENT) {
		printf("</table>\n");

		if (total_notifications == 0) {
			printf("<div class='errorMessage' style='text-align:center;'>No notifications have been recorded");
			if (find_all == FALSE) {
				if (query_type == DISPLAY_SERVICES)
					printf(" for this service");
				else if (query_type == DISPLAY_CONTACTS)
					printf(" for this contact");
				else
					printf(" for this host");
			}
			printf(" in log files for selected date.</div>");
		}

		page_num_selector(result_start, total_notifications, displayed_entries);

	} else if (content_type == JSON_CONTENT) {
		printf("\n]\n");
	}

	return;
}
Exemplo n.º 28
0
/* Authentication (Server side) */
struct vtun_host * auth_server(int fd)
{
        char chal_req[VTUN_CHAL_SIZE], chal_res[VTUN_CHAL_SIZE];	
	char buf[VTUN_MESG_SIZE], *str1, *str2;
        struct vtun_host *h = NULL;
	char *host = NULL;
	int  stage;

        set_title("authentication");

	print_p(fd,"VTUN server ver %s\n",VTUN_VER);

	stage = ST_HOST;

	while( readn_t(fd, buf, VTUN_MESG_SIZE, vtun.timeout) > 0 ){
	   buf[sizeof(buf)-1]='\0';
	   strtok(buf,"\r\n");

	   if( !(str1=strtok(buf," :")) )
	      break;
	   if( !(str2=strtok(NULL," :")) )
	      break;

	   switch( stage ){
	     case ST_HOST:
	        if( !strcmp(str1,"HOST") ){
		   host = strdup(str2);

		   gen_chal(chal_req);
		   print_p(fd,"OK CHAL: %s\n", cl2cs(chal_req));

		   stage = ST_CHAL;
		   continue;
	        }
		break;
	     case ST_CHAL:
	        if( !strcmp(str1,"CHAL") ){
		   if( !cs2cl(str2,chal_res) )
		      break; 
		   
		   if( !(h = find_host(host)) )
		      break;

		   decrypt_chal(chal_res, h->passwd);   		
	
		   if( !memcmp(chal_req, chal_res, VTUN_CHAL_SIZE) ){
		      /* Auth successeful. */

		      /* Lock host */	
		      if( lock_host(h) < 0 ){
		         /* Multiple connections are denied */
		         h = NULL;
		         break;
		      }	
		      print_p(fd,"OK FLAGS: %s\n", bf2cf(h)); 
 		   } else
		      h = NULL;
	        }
		break;
 	   }
	   break;
	}

	if( host )
	   free(host);

	if( !h )
	   print_p(fd,"ERR\n");	

	return h;
}
Exemplo n.º 29
0
/* displays host status */
void display_host(void) {
	host *temp_host;
	hoststatus *temp_hoststatus;
	char last_check[MAX_DATETIME_LENGTH];
	int days;
	int hours;
	int minutes;
	int seconds;
	time_t current_time;
	time_t t;
	char state_duration[48];
	int found;

	/**** MAIN SCREEN (CARD 1) ****/
	printf("<card id='card1' title='Host Status'>\n");
	printf("<p align='center' mode='nowrap'>\n");
	printf("<b>Host '%s'</b><br/>\n", host_name);

	/* find the host */
	temp_host = find_host(host_name);
	temp_hoststatus = find_hoststatus(host_name);
	if(temp_host == NULL || temp_hoststatus == NULL) {

		printf("<b>Error: Could not find host!</b>\n");
		printf("</p>\n");
		printf("</card>\n");
		return;
		}

	/* check authorization */
	if(is_authorized_for_host(temp_host, &current_authdata) == FALSE) {

		printf("<b>Error: Not authorized for host!</b>\n");
		printf("</p>\n");
		printf("</card>\n");
		return;
		}


	printf("<table columns='2' align='LL'>\n");

	printf("<tr><td>Status:</td><td>");
	if(temp_hoststatus->status == HOST_UP)
		printf("UP");
	else if(temp_hoststatus->status == HOST_PENDING)
		printf("PENDING");
	else if(temp_hoststatus->status == HOST_DOWN)
		printf("DOWN");
	else if(temp_hoststatus->status == HOST_UNREACHABLE)
		printf("UNREACHABLE");
	else
		printf("?");
	printf("</td></tr>\n");

	printf("<tr><td>Info:</td><td>%s</td></tr>\n", temp_hoststatus->plugin_output);

	get_time_string(&temp_hoststatus->last_check, last_check, sizeof(last_check) - 1, SHORT_DATE_TIME);
	printf("<tr><td>Last Check:</td><td>%s</td></tr>\n", last_check);

	current_time = time(NULL);
	if(temp_hoststatus->last_state_change == (time_t)0)
		t = current_time - program_start;
	else
		t = current_time - temp_hoststatus->last_state_change;
	get_time_breakdown((unsigned long)t, &days, &hours, &minutes, &seconds);
	snprintf(state_duration, sizeof(state_duration) - 1, "%2dd %2dh %2dm %2ds%s", days, hours, minutes, seconds, (temp_hoststatus->last_state_change == (time_t)0) ? "+" : "");
	printf("<tr><td>Duration:</td><td>%s</td></tr>\n", state_duration);

	printf("<tr><td>Properties:</td><td>");
	found = 0;
	if(temp_hoststatus->checks_enabled == FALSE) {
		printf("%sChecks disabled", (found == 1) ? ", " : "");
		found = 1;
		}
	if(temp_hoststatus->notifications_enabled == FALSE) {
		printf("%sNotifications disabled", (found == 1) ? ", " : "");
		found = 1;
		}
	if(temp_hoststatus->problem_has_been_acknowledged == TRUE) {
		printf("%sProblem acknowledged", (found == 1) ? ", " : "");
		found = 1;
		}
	if(temp_hoststatus->scheduled_downtime_depth > 0) {
		printf("%sIn scheduled downtime", (found == 1) ? ", " : "");
		found = 1;
		}
	if(found == 0)
		printf("N/A");
	printf("</td></tr>\n");

	printf("</table>\n");
	printf("<br/>\n");
	printf("<b><anchor title='View Services'>View Services<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='style' value='servicedetail'/></go></anchor></b>\n", STATUSWML_CGI, escape_string(host_name));
	printf("<b><anchor title='Host Commands'>Host Commands<go href='#card2'/></anchor></b>\n");
	printf("</p>\n");

	printf("</card>\n");


	/**** COMMANDS SCREEN (CARD 2) ****/
	printf("<card id='card2' title='Host Commands'>\n");
	printf("<p align='center' mode='nowrap'>\n");
	printf("<b>Host Commands</b><br/>\n");

	printf("<b><anchor title='Ping Host'>Ping Host<go href='%s' method='post'><postfield name='ping' value='%s'/></go></anchor></b>\n", STATUSWML_CGI, temp_host->address);
	printf("<b><anchor title='Traceroute'>Traceroute<go href='%s' method='post'><postfield name='traceroute' value='%s'/></go></anchor></b>\n", STATUSWML_CGI, temp_host->address);

	if(temp_hoststatus->status != HOST_UP && temp_hoststatus->status != HOST_PENDING)
		printf("<b><anchor title='Acknowledge Problem'>Acknowledge Problem<go href='#card3'/></anchor></b>\n");

	if(temp_hoststatus->checks_enabled == FALSE)
		printf("<b><anchor title='Enable Host Checks'>Enable Host Checks<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_ENABLE_HOST_CHECK, CMDMODE_COMMIT);
	else
		printf("<b><anchor title='Disable Host Checks'>Disable Host Checks<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_DISABLE_HOST_CHECK, CMDMODE_COMMIT);

	if(temp_hoststatus->notifications_enabled == FALSE)
		printf("<b><anchor title='Enable Host Notifications'>Enable Host Notifications<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_ENABLE_HOST_NOTIFICATIONS, CMDMODE_COMMIT);
	else
		printf("<b><anchor title='Disable Host Notifications'>Disable Host Notifications<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_DISABLE_HOST_NOTIFICATIONS, CMDMODE_COMMIT);


	printf("<b><anchor title='Enable All Service Checks'>Enable All Service Checks<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_ENABLE_HOST_SVC_CHECKS, CMDMODE_COMMIT);

	printf("<b><anchor title='Disable All Service Checks'>Disable All Service Checks<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_DISABLE_HOST_SVC_CHECKS, CMDMODE_COMMIT);

	printf("<b><anchor title='Enable All Service Notifications'>Enable All Service Notifications<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_ENABLE_HOST_SVC_NOTIFICATIONS, CMDMODE_COMMIT);

	printf("<b><anchor title='Disable All Service Notifications'>Disable All Service Notifications<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go></anchor></b><br/>\n", COMMAND_CGI, escape_string(host_name), CMD_DISABLE_HOST_SVC_NOTIFICATIONS, CMDMODE_COMMIT);

	printf("</p>\n");

	printf("</card>\n");


	/**** ACKNOWLEDGEMENT SCREEN (CARD 3) ****/
	printf("<card id='card3' title='Acknowledge Problem'>\n");
	printf("<p align='center' mode='nowrap'>\n");
	printf("<b>Acknowledge Problem</b><br/>\n");
	printf("</p>\n");

	printf("<p align='center' mode='wrap'>\n");
	printf("<b>Your Name:</b><br/>\n");
	printf("<input name='name' value='%s' /><br/>\n", ((use_ssl_authentication) ? (getenv("SSL_CLIENT_S_DN_CN")) : (getenv("REMOTE_USER"))));
	printf("<b>Comment:</b><br/>\n");
	printf("<input name='comment' value='acknowledged by WAP'/>\n");

	printf("<do type='accept'>\n");
	printf("<go href='%s' method='post'><postfield name='host' value='%s'/><postfield name='com_author' value='$(name)'/><postfield name='com_data' value='$(comment)'/><postfield name='persistent' value=''/><postfield name='send_notification' value=''/><postfield name='cmd_typ' value='%d'/><postfield name='cmd_mod' value='%d'/><postfield name='content' value='wml'/></go>\n", COMMAND_CGI, escape_string(host_name), CMD_ACKNOWLEDGE_HOST_PROBLEM, CMDMODE_COMMIT);
	printf("</do>\n");

	printf("</p>\n");

	printf("</card>\n");

	return;
	}
Exemplo n.º 30
0
/* displays hostgroup status summary */
void display_hostgroup_summary(void) {
	hostgroup *temp_hostgroup;
	hostsmember *temp_member;
	host *temp_host;
	hoststatus *temp_hoststatus;
	service *temp_service;
	servicestatus *temp_servicestatus;
	int hosts_unreachable = 0;
	int hosts_down = 0;
	int hosts_up = 0;
	int hosts_pending = 0;
	int services_critical = 0;
	int services_unknown = 0;
	int services_warning = 0;
	int services_ok = 0;
	int services_pending = 0;
	int found = 0;


	/**** MAIN SCREEN (CARD 1) ****/
	printf("<card id='card1' title='Status Summary'>\n");
	printf("<p align='center' mode='nowrap'>\n");

	printf("<b><anchor title='Status Summary'>Status Summary<go href='%s' method='post'><postfield name='hostgroup' value='%s'/><postfield name='style' value='overview'/></go></anchor></b><br/><br/>\n", STATUSWML_CGI, escape_string(hostgroup_name));

	/* check all hostgroups */
	for(temp_hostgroup = hostgroup_list; temp_hostgroup != NULL; temp_hostgroup = temp_hostgroup->next) {

		if(show_all_hostgroups == FALSE && strcmp(temp_hostgroup->group_name, hostgroup_name))
			continue;

		if(is_authorized_for_hostgroup(temp_hostgroup, &current_authdata) == FALSE)
			continue;

		printf("<b><anchor title='%s'>%s<go href='%s' method='post'><postfield name='hostgroup' value='%s'/><postfield name='style' value='overview'/></go></anchor></b>\n", temp_hostgroup->group_name, temp_hostgroup->alias, STATUSWML_CGI, temp_hostgroup->group_name);

		printf("<table columns='2' align='LL'>\n");

		hosts_up = 0;
		hosts_pending = 0;
		hosts_down = 0;
		hosts_unreachable = 0;

		services_ok = 0;
		services_pending = 0;
		services_warning = 0;
		services_unknown = 0;
		services_critical = 0;

		/* check all hosts in this hostgroup */
		for(temp_member = temp_hostgroup->members; temp_member != NULL; temp_member = temp_member->next) {

			temp_host = find_host(temp_member->host_name);
			if(temp_host == NULL)
				continue;

			if(is_host_member_of_hostgroup(temp_hostgroup, temp_host) == FALSE)
				continue;

			temp_hoststatus = find_hoststatus(temp_host->name);
			if(temp_hoststatus == NULL)
				continue;

			if(temp_hoststatus->status == HOST_UNREACHABLE)
				hosts_unreachable++;
			else if(temp_hoststatus->status == HOST_DOWN)
				hosts_down++;
			else if(temp_hoststatus->status == HOST_PENDING)
				hosts_pending++;
			else
				hosts_up++;

			/* check all services on this host */
			for(temp_service = service_list; temp_service != NULL; temp_service = temp_service->next) {

				if(strcmp(temp_service->host_name, temp_host->name))
					continue;

				if(is_authorized_for_service(temp_service, &current_authdata) == FALSE)
					continue;

				temp_servicestatus = find_servicestatus(temp_service->host_name, temp_service->description);
				if(temp_servicestatus == NULL)
					continue;

				if(temp_servicestatus->status == SERVICE_CRITICAL)
					services_critical++;
				else if(temp_servicestatus->status == SERVICE_UNKNOWN)
					services_unknown++;
				else if(temp_servicestatus->status == SERVICE_WARNING)
					services_warning++;
				else if(temp_servicestatus->status == SERVICE_PENDING)
					services_pending++;
				else
					services_ok++;
				}
			}

		printf("<tr><td>Hosts:</td><td>");
		found = 0;
		if(hosts_unreachable > 0) {
			printf("%d UNR", hosts_unreachable);
			found = 1;
			}
		if(hosts_down > 0) {
			printf("%s%d DWN", (found == 1) ? ", " : "", hosts_down);
			found = 1;
			}
		if(hosts_pending > 0) {
			printf("%s%d PND", (found == 1) ? ", " : "", hosts_pending);
			found = 1;
			}
		printf("%s%d UP", (found == 1) ? ", " : "", hosts_up);
		printf("</td></tr>\n");
		printf("<tr><td>Services:</td><td>");
		found = 0;
		if(services_critical > 0) {
			printf("%d CRI", services_critical);
			found = 1;
			}
		if(services_warning > 0) {
			printf("%s%d WRN", (found == 1) ? ", " : "", services_warning);
			found = 1;
			}
		if(services_unknown > 0) {
			printf("%s%d UNK", (found == 1) ? ", " : "", services_unknown);
			found = 1;
			}
		if(services_pending > 0) {
			printf("%s%d PND", (found == 1) ? ", " : "", services_pending);
			found = 1;
			}
		printf("%s%d OK", (found == 1) ? ", " : "", services_ok);
		printf("</td></tr>\n");

		printf("</table>\n");

		printf("<br/>\n");
		}

	if(show_all_hostgroups == FALSE)
		printf("<b><anchor title='View All Hostgroups'>View All Hostgroups<go href='%s' method='post'><postfield name='hostgroup' value='all'/><postfield name='style' value='summary'/></go></anchor></b>\n", STATUSWML_CGI);

	printf("</p>\n");

	printf("</card>\n");

	return;
	}