summaryrefslogtreecommitdiffstats
path: root/source3/smbd/perfcount.c
blob: a7c268a2fef234a41f34ab662f87d2b2ecce50b1 (plain)
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
188
189
190
191
192
193
/*
   Unix SMB/Netbios implementation.
   Perfcounter initialization and support functions
   Copyright (C) Todd Stecher 2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "includes.h"
#include "smbd/smbd.h"

static struct smb_perfcount_handlers *g_smb_perfcount_handlers = NULL;

struct smb_perfcount_module {
	char *name;
	struct smb_perfcount_handlers *handlers;
	struct smb_perfcount_module *prev, *next;
};

struct smb_perfcount_module *modules = NULL;


/*
 * a module is registered before it is actually loaded - keep a list
 *
 * @todo - currently perfcount modules are globally configured, so
 * building the list is not strictly required.
 * However, its a proven concept in VFS, and is here to allow a
 * move to eventual per-service perfcount configuration.
 *
 * Note many pre-connection statistics are interesting
 * (e.g. before binding to an individual share).
 *
 */
static struct smb_perfcount_module *smb_perfcount_find_module(const char *name)
{
	struct smb_perfcount_module *entry = modules;

	while (entry) {
		if (strcmp(entry->name, name)==0)
			return entry;

		entry = entry->next;
	}

	return NULL;
}
NTSTATUS smb_register_perfcounter(int interface_version, const char *name,
				  const struct smb_perfcount_handlers *handlers)
{
	struct smb_perfcount_module *entry = modules;

	if (interface_version != SMB_PERFCOUNTER_INTERFACE_VERSION) {
		DEBUG(0, ("Failed to register perfcount module.\n"
		          "The module was compiled against "
			  "SMB_PERFCOUNTER_INTERFACE_VERSION %d,\n"
		          "current SMB_PERFCOUNTER_INTERFACE_VERSION is %d.\n"
		          "Please recompile against the current Samba Version!\n",
			  interface_version, SMB_PERFCOUNTER_INTERFACE_VERSION));
		return NT_STATUS_OBJECT_TYPE_MISMATCH;
	}

	if (!name || !name[0] || !handlers) {
		DEBUG(0,("smb_register_perfcounter() called with NULL pointer "
			"or empty name!\n"));
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (smb_perfcount_find_module(name)) {
		DEBUG(3,("Perfcount Module %s already loaded!\n", name));
		return NT_STATUS_OK;
	}

	entry = SMB_XMALLOC_P(struct smb_perfcount_module);
	entry->name = smb_xstrdup(name);
	entry->handlers = discard_const_p(struct smb_perfcount_handlers, handlers);

	DLIST_ADD(modules, entry);
	DEBUG(3, ("Successfully added perfcounter module '%s'\n", name));
	return NT_STATUS_OK;
}

/****************************************************************************
  initialise smb perf counters
 ****************************************************************************/
static bool smb_load_perfcount_module(const char *name)
{
	char *module_path = NULL;
	char *module_name = NULL;
	char *module_param = NULL, *p;

	const struct smb_perfcount_module *entry;

	DEBUG(3, ("Initialising perfcounter module [%s]\n", name));

	if (g_smb_perfcount_handlers) {
		DEBUG(3,("Only 1 perfcount handler may be registered in "
			"smb.conf\n"));
		return true;
	}

	module_path = smb_xstrdup(name);

	p = strchr_m(module_path, ':');

	if (p) {
		*p = 0;
		module_param = p+1;
		trim_char(module_param, ' ', ' ');
	}

	trim_char(module_path, ' ', ' ');

	module_name = smb_xstrdup(module_path);

	if (module_name[0] == '/') {

		/*
		 * Extract the module name from the path. Just use the base
		 * name of the last path component.
		 */

		SAFE_FREE(module_name);
		module_name = smb_xstrdup(strrchr_m(module_path, '/')+1);

		p = strchr_m(module_name, '.');

		if (p != NULL) {
			*p = '\0';
		}
	}

	/* load the perfcounter module */
	if((entry = smb_perfcount_find_module(module_name)) ||
	   (NT_STATUS_IS_OK(smb_probe_module("perfcount", module_path)) &&
		(entry = smb_perfcount_find_module(module_name)))) {

		DEBUG(3,("Successfully loaded perfcounter module [%s] \n", name));
	} else {
		DEBUG(0,("Can't find a perfcounter module [%s]\n",name));
		goto fail;
	}

	g_smb_perfcount_handlers = entry->handlers;

	SAFE_FREE(module_path);
	SAFE_FREE(module_name);
	return True;

 fail:
	SAFE_FREE(module_path);
	SAFE_FREE(module_name);
	return False;
}

void smb_init_perfcount_data(struct smb_perfcount_data *pcd)
{

	ZERO_STRUCTP(pcd);
	pcd->handlers = g_smb_perfcount_handlers;
}

bool smb_perfcount_init(void)
{
	char *perfcount_object;

	perfcount_object = lp_perfcount_module(talloc_tos());

	/* don't init */
	if (!perfcount_object || !perfcount_object[0])
		return True;

	if (!smb_load_perfcount_module(perfcount_object)) {
		DEBUG(0, ("smbd_load_percount_module failed for %s\n",
			perfcount_object));
		return False;
	}


	return True;
}