File: keywords.c

package info (click to toggle)
cacti-spine 0.8.7e-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,900 kB
  • ctags: 534
  • sloc: ansic: 4,564; sh: 882; makefile: 70
file content (187 lines) | stat: -rw-r--r-- 6,791 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 ex: set tabstop=4 shiftwidth=4 autoindent:
 +-------------------------------------------------------------------------+
 | Copyright (C) 2002-2008 The Cacti Group                                 |
 |                                                                         |
 | This program is free software; you can redistribute it and/or           |
 | modify it under the terms of the GNU Lesser General Public              |
 | License as published by the Free Software Foundation; either            |
 | version 2.1 of the License, or (at your option) any later version. 	   |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU Lesser General Public License for more details.                     |
 |                                                                         |
 | You should have received a copy of the GNU Lesser General Public        |
 | License along with this library; if not, write to the Free Software     |
 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA           |
 | 02110-1301, USA                                                         |
 |                                                                         |
 +-------------------------------------------------------------------------+
 | spine: a backend data gatherer for cacti                                |
 +-------------------------------------------------------------------------+
 | This poller would not have been possible without:                       |
 |   - Larry Adams (current development and enhancements)                  |
 |   - Rivo Nurges (rrd support, mysql poller cache, misc functions)       |
 |   - RTG (core poller code, pthreads, snmp, autoconf examples)           |
 |   - Brady Alleman/Doug Warner (threading ideas, implimentation details) |
 +-------------------------------------------------------------------------+
 | - Cacti - http://www.cacti.net/                                         |
 +-------------------------------------------------------------------------+
*/
/*!
 *
 *	This module provides keyword-lookup support for various spine
 *	objects. The idea is that we can do a two-way translation: given
 *	a token, return a printable name for it, and to take a word from
 *	the user and return the numeric internal value.
 *
 *	The center of the module is the table of keywords which map in
 *	both directions word<-->value. Lookups are case insensitive, and
 *	both direction
 *
*/

#include "common.h"
#include "spine.h"

struct keyword {
	const char *word;
	int         value;
};

/*! Log Level Structure
 *
 *	Structure that helps map either an integer value to a text logging level or
 *	vice versa.
 *
 */
static const struct keyword log_level[] = {
	{ "NONE",   POLLER_VERBOSITY_NONE   },
	{ "LOW",    POLLER_VERBOSITY_LOW    },
	{ "MEDIUM", POLLER_VERBOSITY_MEDIUM },
	{ "HIGH",   POLLER_VERBOSITY_HIGH   },
	{ "DEBUG",  POLLER_VERBOSITY_DEBUG  },

	{ 0, 0 }	/* ENDMARKER */
};

/*! Log Destination Structure
 *
 *	Structure that helps map either an integer value to a text logging destination
 *  or vice versa.
 *
 */
static const struct keyword logdest[] = {
	{ "FILE",   LOGDEST_FILE   },
	{ "SYSLOG", LOGDEST_SYSLOG },
	{ "BOTH",   LOGDEST_BOTH   },
	{ "STDOUT", LOGDEST_STDOUT },

	{ 0, 0 }	/* ENDMARKER */
};

/*! Poller Action Structure
 *
 *	Structure that helps map either an integer value to a text poller action
 *  or vice versa.
 *
 */
static const struct keyword actions[] = {
	{ "SNMP",       POLLER_ACTION_SNMP               },
	{ "SCRIPT",     POLLER_ACTION_SCRIPT             },
	{ "PHPSCRIPT",	POLLER_ACTION_PHP_SCRIPT_SERVER  },

	{ 0, 0 }	/* ENDMARKER */
};

/*! \fn find_keyword_by_word(const struct keyword *tbl, const char *word, int dflt)
 *  \brief takes a generic word and returns either TRUE or FALSE
 *  \param tbl the table that contains the translation from text to boolean
 *  \param word the word to compare against the table for the result
 *  \param dflt the default value to be returned if the string can not be found
 *
 *	Given a table of keywords and a user's word, look that word up in the
 *	table and return the value associted with it. If the word is not found,
 *	return the user-provide default value.
 *
 *	The default-value parameter can be used for either the actual default
 *	value of the parameter being searched for (say, LOGDEST_BOTH), or
 *	a didn't-find-it value (say, -1) which the caller can key off of.
 *
 *	NOTE: if the given word is all digits, it's parsed as a number and
 *	returned numerically.
 *
 *  \return TRUE, FALSE, or dflt depending on results of search
 *
 */
static int find_keyword_by_word(const struct keyword *tbl, const char *word, int dflt)
{
	assert(tbl  != 0);
	assert(word != 0);

	if (all_digits(word)) {
		return atoi(word);
	}

	for (; tbl->word; tbl++) {
		if (STRIMATCH(word, tbl->word)) {
			return tbl->value;
		}
	}

	return dflt;
}

/*! \fn static const char *find_keyword_by_value(const struct keyword *tbl, int value, const char *dflt)
 *  \brief searches a table for text string based upon a numeric input value
 *  \param tbl the table that contains the translation from text to boolean
 *  \param word the word to compare against the table for the result
 *  \param dflt the default value to be returned if the string can not be found
 *
 *	Given a keyword table and a numeric value, find the printable word
 *	associated with it. The *first* value found is returned (in case more
 *	than one word maps to the same value), and if it's not found, the
 *	user's default value is returned.
 *
 *	The dflt value is allowed to be NULL.
 *
 *  \return a string pointer to that matches the search criteria, or dflt
 *
 */
static const char *find_keyword_by_value(const struct keyword *tbl, int value, const char *dflt) {
	assert(tbl != 0);

	for (; tbl->word; tbl++ ) {
		if (tbl->value == value) {
			return tbl->word;
		}
	}

	return dflt;
}

const char *printable_log_level(int token) {
	return find_keyword_by_value(log_level, token, "-unknown-");
}

int parse_log_level(const char *word, int dflt) {
	return find_keyword_by_word(log_level, word, dflt);
}

const char *printable_logdest(int token) {
	return find_keyword_by_value(logdest, token, "-unknown-");
}

int parse_logdest(const char *word, int dflt) {
	return find_keyword_by_word(logdest, word, dflt);
}

const char *printable_action(int token) {
	return find_keyword_by_value(actions, token, "-unknown-");
}

int parse_action(const char *word, int dflt) {
	return find_keyword_by_word(actions, word, dflt);
}