db_updater: Put parentheses back
[merlin.git] / oconf.c
blob1186a64fd5d4b37caad1620cf6646bed981f28b2
1 #include "module.h"
3 /*
4 * the code in misc.c expects this to be a global variable. Yes, that's
5 * weird, and no I'm not gonna do anything about it right now. it is a
6 * global variable in Nagios and the module is the designated user of
7 * that functionality, so it's not, strictly speaking, necessary.
8 */
9 char *config_file = NULL;
12 * converts an arbitrarily long string of data into its
13 * hexadecimal representation
15 char *tohex(const unsigned char *data, int len)
17 /* number of bufs must be a power of 2 */
18 static char bufs[4][41], hex[] = "0123456789abcdef";
19 static int bufno;
20 char *buf;
21 int i;
23 buf = bufs[bufno & (ARRAY_SIZE(bufs) - 1)];
24 for (i = 0; i < 20 && i < len; i++) {
25 unsigned int val = *data++;
26 *buf++ = hex[val >> 4];
27 *buf++ = hex[val & 0xf];
29 *buf = '\0';
31 return bufs[bufno++ & (ARRAY_SIZE(bufs) - 1)];
34 #define CMD_HASH 1
35 #define CMD_LAST_CHANGE 2
36 #define CMD_FILES 3
37 int main(int argc, char **argv)
39 unsigned char hash[20];
40 int i, cmd = 0;
41 char *base, *cmd_string = NULL;
43 base = strrchr(argv[0], '/');
44 if (!base)
45 base = argv[0];
46 else
47 base++;
49 if (!prefixcmp(base, "oconf.")) {
50 cmd_string = base + strlen("oconf.");
51 if (!strcmp(cmd_string, "changed")) {
52 cmd = CMD_LAST_CHANGE;
53 } else if (!strcmp(cmd_string, "hash")) {
54 cmd = CMD_HASH;
58 for (i = 1; i < argc; i++) {
59 char *arg = argv[i];
60 if (!prefixcmp(arg, "--nagios-cfg=")) {
61 config_file = &arg[strlen("--nagios-cfg=")];
62 continue;
64 while (*arg == '-')
65 arg++;
67 if (!prefixcmp(arg, "last") || !prefixcmp(arg, "change")) {
68 cmd = CMD_LAST_CHANGE;
69 continue;
71 if (!strcmp(arg, "sha1") || !strcmp(arg, "hash")) {
72 cmd = CMD_HASH;
73 continue;
75 if (!prefixcmp(arg, "list") || !strcmp(arg, "files")) {
76 cmd = CMD_FILES;
77 continue;
80 if (!config_file)
81 config_file = "/opt/monitor/etc/nagios.cfg";
83 switch (cmd) {
84 case CMD_HASH:
85 get_config_hash(hash);
86 printf("%s\n", tohex(hash, 20));
87 break;
88 case CMD_LAST_CHANGE:
89 printf("%lu\n", get_last_cfg_change());
90 break;
91 case CMD_FILES:
93 struct file_list **sorted_flist;
94 unsigned int num_files, i;
96 sorted_flist = get_sorted_oconf_files(&num_files);
97 if(!sorted_flist)
98 break;
100 for (i = 0; i < num_files; i++) {
101 printf("%s\n", sorted_flist[i]->name);
102 sorted_flist[i]->next = NULL;
103 file_list_free(sorted_flist[i]);
105 free(sorted_flist);
107 break;
109 return 0;