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, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern sig_atomic_t keep_running;
32 void add_key_raw(TDB_CONTEXT *db, char *keystring, void *databuf, size_t datasize, int flags)
37 key.dsize = strlen(keystring);
39 data.dsize = datasize;
41 tdb_store(db, key, data, flags);
44 void add_key(TDB_CONTEXT *db, char *keystring, char *datastring, int flags)
49 key.dsize = strlen(keystring);
50 data.dptr = datastring;
51 data.dsize = strlen(datastring);
53 tdb_store(db, key, data, flags);
56 void make_key(char *buf, int buflen, int key_part1, char *key_part2)
58 memset(buf, 0, buflen);
60 sprintf(buf, "%d%s", key_part1, key_part2);
62 sprintf(buf, "%d", key_part1);
67 void usage(char *progname)
69 fprintf(stderr, "Usage: %s [-d] [-f <file_path>].\n", progname);
70 fprintf(stderr, "\t-d: run as a daemon.\n");
71 fprintf(stderr, "\t-f <file_path>: path where the TDB files reside.\n");
72 fprintf(stderr, "\t\tDEFAULT is /var/lib/samba/perfmon\n");
76 void parse_flags(RuntimeSettings *rt, int argc, char **argv)
80 while((flag = getopt(argc, argv, "df:")) != -1)
91 memcpy(rt->dbDir, optarg, strlen(optarg));
104 void setup_file_paths(RuntimeSettings *rt)
108 if(strlen(rt->dbDir) == 0)
110 /* No file path was passed in, use default */
111 sprintf(rt->dbDir, "/var/lib/samba/perfmon");
114 sprintf(rt->nameFile, "%s/names.tdb", rt->dbDir);
115 sprintf(rt->counterFile, "%s/data.tdb", rt->dbDir);
117 mkdir(rt->dbDir, 0755);
118 rt->cnames = tdb_open(rt->nameFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);
119 rt->cdata = tdb_open(rt->counterFile, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT, 0644);
121 if(rt->cnames == NULL || rt->cdata == NULL)
123 perror("setup_file_paths");
130 void sigterm_handler()
132 keep_running = FALSE;
136 void daemonize(RuntimeSettings *rt)
142 /* Check if we're already a daemon */
151 /* we're the parent */
152 tdb_close(rt->cnames);
153 tdb_close(rt->cdata);
157 /* get a new session */
164 /* close file descriptors */
166 close(STDOUT_FILENO);
167 close(STDERR_FILENO);
169 /* And reopen them as safe defaults */
170 fd = open("/dev/null", O_RDONLY);
176 fd = open("/dev/null", O_WRONLY);
182 fd = open("/dev/null", O_WRONLY);
190 signal(SIGINT, SIG_IGN);
191 signal(SIGHUP, SIG_IGN);
192 signal(SIGTERM, sigterm_handler);
197 int get_counter_id(PERF_DATA_BLOCK *data)
199 data->counter_id += 2;
200 data->num_counters++;
202 return data->counter_id;
205 void init_perf_counter(PerfCounter *counter,
213 counter->index = index;
214 memcpy(counter->name, name, strlen(name));
215 memcpy(counter->help, help, strlen(help));
216 counter->counter_type = counter_type;
217 counter->record_type = record_type;
222 sprintf(counter->relationships, "p");
225 sprintf(counter->relationships, "c[%d]", parent->index);
228 sprintf(counter->relationships, "i[%d]", parent->index);
231 perror("init_perf_counter: unknown record type");