2 * Unix SMB/CIFS implementation.
3 * Performance Counter Daemon
5 * Copyright (C) Marcin Krzysztof Porwit 2005
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 extern sig_atomic_t keep_running;
31 void add_key_raw(TDB_CONTEXT *db, char *keystring, void *databuf, size_t datasize, int flags)
36 key.dsize = strlen(keystring);
38 data.dsize = datasize;
40 tdb_store(db, key, data, flags);
43 void add_key(TDB_CONTEXT *db, char *keystring, char *datastring, int flags)
48 key.dsize = strlen(keystring);
49 data.dptr = datastring;
50 data.dsize = strlen(datastring);
52 tdb_store(db, key, data, flags);
55 void make_key(char *buf, int buflen, int key_part1, char *key_part2)
57 memset(buf, 0, buflen);
59 sprintf(buf, "%d%s", key_part1, key_part2);
61 sprintf(buf, "%d", key_part1);
66 void usage(char *progname)
68 fprintf(stderr, "Usage: %s [-d] [-f <file_path>].\n", progname);
69 fprintf(stderr, "\t-d: run as a daemon.\n");
70 fprintf(stderr, "\t-f <file_path>: path where the TDB files reside.\n");
71 fprintf(stderr, "\t\tDEFAULT is /var/lib/samba/perfmon\n");
75 void parse_flags(RuntimeSettings *rt, int argc, char **argv)
79 while((flag = getopt(argc, argv, "df:")) != -1)
90 memcpy(rt->dbDir, optarg, strlen(optarg));
103 void setup_file_paths(RuntimeSettings *rt)
107 if(strlen(rt->dbDir) == 0)
109 /* No file path was passed in, use default */
110 sprintf(rt->dbDir, "/var/lib/samba/perfmon");
113 sprintf(rt->nameFile, "%s/names.tdb", rt->dbDir);
114 sprintf(rt->counterFile, "%s/data.tdb", rt->dbDir);
116 mkdir(rt->dbDir, 0755);
117 rt->cnames = tdb_open(rt->nameFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);
118 rt->cdata = tdb_open(rt->counterFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);
120 if(rt->cnames == NULL || rt->cdata == NULL)
122 perror("setup_file_paths");
129 void sigterm_handler()
131 keep_running = FALSE;
135 void daemonize(RuntimeSettings *rt)
141 /* Check if we're already a daemon */
150 /* we're the parent */
151 tdb_close(rt->cnames);
152 tdb_close(rt->cdata);
156 /* get a new session */
163 /* close file descriptors */
165 close(STDOUT_FILENO);
166 close(STDERR_FILENO);
168 /* And reopen them as safe defaults */
169 fd = open("/dev/null", O_RDONLY);
175 fd = open("/dev/null", O_WRONLY);
181 fd = open("/dev/null", O_WRONLY);
189 signal(SIGINT, SIG_IGN);
190 signal(SIGHUP, SIG_IGN);
191 signal(SIGTERM, sigterm_handler);
196 int get_counter_id(PERF_DATA_BLOCK *data)
198 data->counter_id += 2;
199 data->num_counters++;
201 return data->counter_id;
204 void init_perf_counter(PerfCounter *counter,
212 counter->index = index;
213 memcpy(counter->name, name, strlen(name));
214 memcpy(counter->help, help, strlen(help));
215 counter->counter_type = counter_type;
216 counter->record_type = record_type;
221 sprintf(counter->relationships, "p");
224 sprintf(counter->relationships, "c[%d]", parent->index);
227 sprintf(counter->relationships, "i[%d]", parent->index);
230 perror("init_perf_counter: unknown record type");