/*-
 * Copyright (c) 2003 Andrey Simonenko
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *   @(#)$Id: ipa_thresholds.h,v 1.1.4.2 2007/02/17 09:21:54 simon Exp $
 */

#ifndef IPA_THRESHOLD_H
#define IPA_THRESHOLD_H

#ifdef WITH_THRESHOLDS

#ifndef THRESHOLD_NSIZE
# define THRESHOLD_NSIZE 30
#endif

#ifndef THRESHOLD_NALLOC
# define THRESHOLD_NALLOC 20
#endif

#define THRESHOLD_JUMP_OVER_INACTIVE	0x1
#define THRESHOLD_JUMP_OVER_STOPPED	0x2
#define THRESHOLD_JUMP_OVER_ALLBITS	(THRESHOLD_JUMP_OVER_INACTIVE|THRESHOLD_JUMP_OVER_STOPPED)

struct rule;

/*
 * rule { threshold {}} section.
 */
struct threshold {
	STAILQ_ENTRY(threshold) link;	/* Link for list of thresholds. */

	char		*threshold_name;/* Name of this threshold. */
	char		*threshold_info;/* threshold { info } */
	u_int		thresholdno;	/* Order number of this threshold in its rule. */

	uint64_t	thr;		/* threshold { threshold } */
	uint64_t	thr_dev;	/* threshold { threshold_deviation } */
	u_int		thr_dev_per_cent;/* threshold { threshold_deviation } if it is xx%. */
	uint64_t	thr_max;	/* thr + thr_dev */
	uint64_t	thr_min;	/* thr - thr_dev */
	uint64_t	cnt;		/* Counter. */
	uint64_t	cnt_neg;	/* Negative counter. */
	u_int		cnt_type;	/* Type of the "threshold" parameter's argument. */

	uint64_t	*cnt_slice;	/* Counters for time slices. */
	BITMAP_TYPE	*cnt_slice_sign;/* Signs for cnt_slice. */
	u_int		cnt_slice_n;	/* Number of time slices. */
	u_int		cnt_slice_i;	/* Current index in cnt_slice. */

	int		threshold_type;	/* threshold { threshold_type } */
	int		load_threshold;	/* threshold { load_threshold } */

	u_int		time_width;	/* threshold { threshold_time_width } */
	const struct tevent *time_slice;/* threshold { threshold_time_slice } */

	const struct worktime *worktime;/* threshold { worktime } */
	int		is_active;	/* [IN]ACTIVE_FLAG */

	const struct db_list *db_list;	/* threshold { db_list } */

	u_int		below_lim;	/* X:-:- threshold { threshold_balance } */
	u_int		equal_lim;	/* -:X:- threshold { threshold_balance } */
	u_int		above_lim;	/* -:-:X threshold { threshold_balance } */

	u_int		below_cnt;	/* Rest from below_lim. */
	u_int		equal_cnt;	/* Rest from equal_lim. */
	u_int		above_cnt;	/* Rest from above_lim. */

	struct cmd_list below_threshold;/* threshold { below_threshold {}} */
	struct cmd_list equal_threshold;/* threshold { equal_threshold {}} */
	struct cmd_list above_threshold;/* threshold { above_threshold {}} */

	struct cmd_list	rc[2];		/* threshold { startup|shutdown {}} */

	struct wpid	wpid;		/* Threshold's wpid structure. */

	int		update_tm_from;	/* If non-zero, then update tm_from. */
	ipa_tm		tm_from;	/* Time when threshold started (dynamically changed). */
	ipa_tm		tm_updated;	/* Time when threshold was updated (dynamically changed). */
	u_int		check_sec;	/* Time when to check threshold. */

	const struct rule *rule;	/* Pointer to threshold's rule. */
};

/*
 * List of all thresholds in one rule.
 */
STAILQ_HEAD(thresholds_list, threshold);

extern const char *const threshold_event_msg[];

extern int	global_debug_threshold;
extern int	global_debug_threshold_init;

extern int	global_threshold_type;
extern int	global_load_threshold;
extern u_int	global_threshold_time_width;
extern const struct tevent *global_threshold_time_slice;

extern u_int	global_threshold_below_lim;
extern u_int	global_threshold_equal_lim;
extern u_int	global_threshold_above_lim;

extern ipa_mzone *threshold_mzone;

extern void	set_thr_min_max(struct threshold *);
extern int	init_thresholds(const struct rule *);
extern int	check_thresholds_events(const struct rule *, u_int *);
extern int	thresholds_newday(struct rule *);

extern int	add_chunk_to_threshold(const struct rule *, struct threshold *, const uint64_t *);
extern int	add_chunk_to_thresholds(const struct rule *, const uint64_t *);

extern int	sub_chunk_from_threshold(const struct rule *, struct threshold *, const uint64_t *);
extern int	sub_chunk_from_thresholds(const struct rule *, const uint64_t *);

#define set_threshold_inactive(r, t) mod_set_threshold_active((r), (t), INACTIVE_FLAG)
extern int	mod_set_threshold_active(const struct rule *, struct threshold *, int);

extern int	copy_thresholds(struct rule *, const struct thresholds_list *, int);
extern void	free_thresholds(u_int, struct thresholds_list *, int);

extern void	init_cmds_in_threshold(struct threshold *);
extern void	set_sync_exec_in_threshold(struct threshold *);

extern struct threshold *threshold_by_name(const struct rule *, const char *);

#endif /* WITH_THRESHOLDS */

extern u_int	nstatthresholds;
extern u_int	ndynthresholds;

#endif /* !IPA_THRESHOLD_H */


syntax highlighted by Code2HTML, v. 0.9.1