1 /*-
2  * Copyright (c) 2003 Andrey Simonenko
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *   @(#)$Id: ipa_thresholds.h,v 1.2 2011/01/23 18:42:35 simon Exp $
27  */
28 
29 #ifndef IPA_THRESHOLD_H
30 #define IPA_THRESHOLD_H
31 
32 #ifdef WITH_THRESHOLDS
33 
34 #ifndef THRESHOLD_NSIZE
35 # define THRESHOLD_NSIZE 30
36 #endif
37 
38 #ifndef THRESHOLD_NALLOC
39 # define THRESHOLD_NALLOC 20
40 #endif
41 
42 #define THRESHOLD_JUMP_OVER_INACTIVE	0x1
43 #define THRESHOLD_JUMP_OVER_STOPPED	0x2
44 #define THRESHOLD_JUMP_OVER_ALLBITS	(THRESHOLD_JUMP_OVER_INACTIVE | \
45 					 THRESHOLD_JUMP_OVER_STOPPED)
46 
47 #define THRESHOLD_FLAG_ACTIVE	0x1	/* Threshold is active. */
48 #define THRESHOLD_FLAG_SET	0x2	/* "threshold" parameter was given. */
49 #define THRESHOLD_FLAG_INITED	0x4	/* Threshold was initialized. */
50 
51 #define THRESHOLD_IS_ACTIVE(x)	  ((x)->thr_flags & THRESHOLD_FLAG_ACTIVE)
52 #define THRESHOLD_IS_INACTIVE(x)  (!THRESHOLD_IS_ACTIVE(x))
53 #define THRESHOLD_IS_SET(x)	  ((x)->thr_flags & THRESHOLD_FLAG_SET)
54 #define THRESHOLD_IS_NOTSET(x)	  (!THRESHOLD_IS_SET(x))
55 #define THRESHOLD_IS_INITED(x)	  ((x)->thr_flags & THRESHOLD_FLAG_INITED)
56 
57 #define THRESHOLD_SET_ACTIVE(x)	  ((x)->thr_flags |= THRESHOLD_FLAG_ACTIVE)
58 #define THRESHOLD_SET_INACTIVE(x) ((x)->thr_flags &= ~THRESHOLD_FLAG_ACTIVE)
59 #define THRESHOLD_SET_INITED(x)	  ((x)->thr_flags |= THRESHOLD_FLAG_INITED)
60 
61 struct rule;
62 
63 /*
64  * rule { threshold {}} section.
65  * Fields tm_started and tm_updated are dynamically changed.
66  */
67 struct threshold {
68 	STAILQ_ENTRY(threshold) link;	/* Link for list of thresholds. */
69 
70 	unsigned int	no;		/* Threshold ordinal number. */
71 
72 	unsigned int	thr_flags;	/* ORed THRESHOLD_FLAG_xxx */
73 
74 	uint64_t	thr_max;	/* thr + thr_dev */
75 	uint64_t	thr_min;	/* thr - thr_dev */
76 	uint64_t	cnt;		/* Positive counter. */
77 	uint64_t	cnt_neg;	/* Negative counter. */
78 	uint64_t	*cnt_slice;	/* Counters for time slices. */
79 	BITMAP_TYPE	*cnt_slice_sign;/* Signs for cnt_slice. */
80 	unsigned int	cnt_slice_n;	/* Number of time slices. */
81 	unsigned int	cnt_slice_i;	/* Current index in cnt_slice. */
82 
83 	unsigned int	time_width;	/* { threshold_time_width } */
84 	const struct tevent *time_slice;/* { threshold_time_slice } */
85 	const struct worktime *worktime;/* { worktime } */
86 
87 	const struct db_list *db_list;	/* { db_list } */
88 
89 	unsigned int	below_lim;	/* X:-:- { threshold_balance } */
90 	unsigned int	equal_lim;	/* -:X:- { threshold_balance } */
91 	unsigned int	above_lim;	/* -:-:X { threshold_balance } */
92 	unsigned int	below_cnt;	/* Rest from below_lim. */
93 	unsigned int	equal_cnt;	/* Rest from equal_lim. */
94 	unsigned int	above_cnt;	/* Rest from above_lim. */
95 
96 	struct cmds	below_thr;	/* { below_threshold {}} */
97 	struct cmds	equal_thr;	/* { equal_threshold {}} */
98 	struct cmds	above_thr;	/* { above_threshold {}} */
99 
100 	int		shift_window;	/* If set, then update tm_started. */
101 	ipa_tm		tm_started;	/* Time when time window was started. */
102 	ipa_tm		tm_updated;	/* Time when threshold was updated. */
103 	unsigned int	check_sec;	/* Time when to check threshold. */
104 
105 	struct wpid	wpid;		/* Threshold's wpid structure. */
106 
107 	const struct rule *rule;	/* Pointer to threshold's rule. */
108 
109 	uint64_t	thr;		/* { threshold } */
110 	uint64_t	thr_dev;	/* { threshold_deviation } */
111 	unsigned int	thr_dev_pc;	/* { threshold_deviation } in xx%. */
112 
113 	char		*name;		/* Name of this threshold. */
114 	char		*info;		/* { info } */
115 
116 	unsigned char	cnt_type;	/* Type of "threshold" parameter. */
117 	signed char	load_thr;	/* { load_threshold } */
118 	signed char	thr_type;	/* { threshold_type } */
119 
120 	struct cmds	rc[2];		/* { startup|shutdown {}} */
121 };
122 
123 /*
124  * List of all thresholds in one rule.
125  */
126 STAILQ_HEAD(thresholds_list, threshold);
127 
128 extern const char *const threshold_event_msg[];
129 
130 extern signed char global_debug_threshold;
131 extern signed char global_debug_threshold_init;
132 extern signed char global_threshold_type;
133 extern signed char global_load_threshold;
134 extern unsigned int global_threshold_time_width;
135 extern const struct tevent *global_threshold_time_slice;
136 
137 extern unsigned int global_threshold_below_lim;
138 extern unsigned int global_threshold_equal_lim;
139 extern unsigned int global_threshold_above_lim;
140 
141 extern ipa_mzone *threshold_mzone;
142 
143 extern void	set_thr_min_max(struct threshold *);
144 extern int	init_thresholds(const struct rule *);
145 extern int	check_thresholds(const struct rule *, unsigned int *);
146 extern int	thresholds_newday(struct rule *);
147 
148 extern int	threshold_add_chunk(const struct rule *, struct threshold *,
149 		    const uint64_t *);
150 extern int	threshold_sub_chunk(const struct rule *, struct threshold *,
151 		    const uint64_t *);
152 extern int	thresholds_add_chunk(const struct rule *, const uint64_t *);
153 extern int	thresholds_sub_chunk(const struct rule *, const uint64_t *);
154 
155 #define set_threshold_inactive(r, t) mod_set_threshold_active((r), (t), 0)
156 extern int	mod_set_threshold_active(const struct rule *,
157 		    struct threshold *, int);
158 
159 extern int	copy_thresholds(struct rule *, const struct thresholds_list *);
160 extern void	free_thresholds(unsigned int, struct thresholds_list *, int);
161 
162 extern void	threshold_init_cmds(struct threshold *);
163 extern void	threshold_inherit(struct threshold *threshold);
164 
165 extern struct threshold *threshold_by_name(const struct rule *, const char *);
166 
167 #endif /* WITH_THRESHOLDS */
168 
169 extern unsigned int nstatthresholds;
170 extern unsigned int ndynthresholds;
171 
172 #endif /* !IPA_THRESHOLD_H */
173