aboutsummaryrefslogblamecommitdiffstats
path: root/lib/snmp/src/manager/snmpm.erl
blob: 7e801ebc40fc07a660583777798afca1b6de13b3 (plain) (tree)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501


                   
                                                        




















































































































































































































































































































































































































































































                                                                                 

                                                         





                                                            
                                                                       

                                                                  




                                                                      
 





                                                                            


                        

                                               
                                                                          
                                                  


                                                          

                                                        

                                                

                                   
                                                   





                                                                            

                                                           




                                                            

                                   
                                                            



                 
                                                      

                                   
                                                                       














                                                          





                                                             


                                                                      




                                                                      
 





                                                                            






                                                                           
                                                                               



                                                                          
 

                                                

                                   
                                                    






                                                                           


                                                            
 
                                           

                                   
                                                            



                 
                                                      

                                   
                                                                       











                                                              






                                                                
                                                 
                             

                                                                       
 












                                                                           





                                                

                                                   






                                                         
 




                                                                            

                                   
                                                        


                 


                                                             
 
                                            

                                   
                                                                 



                 
                                                       

                                   
                                                                            











                                                               






                                                                 
                                                  
                             

                                                                        












                                                                            




                                                 

                                                  






                                                         
 







                                                             

                                   
                                                         

                 
         
 
                                            

                                   
                                                                 



                 
                                                       

                                   
                                                                            











                                                                

                                                          





















                                                                             








                                                         

                                                          



                                                               
 





                                                                  










                                                                   
 
                                                  

                                   
                                                                   



                 
                                                             

                                   
                                                                              











                                                                 

                                                         





















                                                                             





                                                       


                                                               


                                                          


                                                           
 
 
                                            
                                




                                                                  

                                   
                                                           


                 


                                                                   
 
                                                  

                                   
                                                                   



                 
                                                             

                                   
                                                                              












                                                                              








                                                                 

                                  
                       
                                        

                                                   

                                                            

                                  



                                                   
 







                                                                       
 






                                                                             
                           

                                                                             




                                                                





                                                                         





                                                                   
                                           

                                  
                       
                                                                    
 







                                                                            



                                  

                                                                             


                                  


                                   
                                                                        

                 
        
 
                                                            


                                              
                                                             



                 
                                                                       


                                              
                                                                        











                                                                               





                                                                 














                                                                   
 


                                                                           
                                                    



                                                                       
 






                                                                             

                                                    
                                                                             




                                                                 





                                                                         





                                                                    
                                            

                                  
                       
                                                                     
 







                                                                            



                                  

                                                                     


                                  



                                               
                                                     

                 
        
 
                                                            


                                               
                                                             



                 
                                                                       


                                               
                                                                        










































































































































































































































































































































                                                                              
%% 
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2004-2011. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%% 

-module(snmpm).

%%----------------------------------------------------------------------
%% This module implements a simple SNMP manager for Erlang.
%%----------------------------------------------------------------------

%% User interface
-export([
	 %% 
	 %% Management API
	 start/0, start/1, 
	 start_link/0, start_link/1, 
	 stop/0, 

	 monitor/0, demonitor/1, 
	 notify_started/1, cancel_notify_started/1, 

	 backup/1, 

	 load_mib/1, unload_mib/1, 
	 which_mibs/0, 
	 name_to_oid/1, oid_to_name/1, oid_to_type/1, 

	 register_user/3, register_user/4, 
	 register_user_monitor/3, register_user_monitor/4, 
	 unregister_user/1, 
	 which_users/0, 

	 register_agent/2, register_agent/3, register_agent/4, 
	 unregister_agent/2, unregister_agent/3,
	 which_agents/0, which_agents/1, 
	 agent_info/2, update_agent_info/4, 
	 
	 register_usm_user/3, unregister_usm_user/2, 
	 which_usm_users/0, which_usm_users/1, 
	 usm_user_info/3, update_usm_user_info/4, 
	 
	 %% 
	 %% Basic SNMP API
	 sync_get/3, sync_get/4, sync_get/5, sync_get/6, 
	 async_get/3, async_get/4, async_get/5, async_get/6, 
	 sync_get_next/3, sync_get_next/4, sync_get_next/5, sync_get_next/6, 
	 async_get_next/3, async_get_next/4, async_get_next/5, async_get_next/6, 
	 sync_set/3, sync_set/4, sync_set/5, sync_set/6, 
	 async_set/3, async_set/4, async_set/5, async_set/6, 
	 sync_get_bulk/5, sync_get_bulk/6, sync_get_bulk/7, sync_get_bulk/8, 
	 async_get_bulk/5, async_get_bulk/6, async_get_bulk/7, async_get_bulk/8, 
	 cancel_async_request/2, 
	 
	 %% 
	 %% Extended SNMP API
	 %% discovery/2, discovery/3, discovery/4, discovery/5, discovery/6, 

	 %% 
	 %% Logging
	 log_to_txt/2, log_to_txt/3, log_to_txt/4,
	 log_to_txt/5, log_to_txt/6, log_to_txt/7, 
	 change_log_size/1,
	 get_log_type/0,
	 set_log_type/1,

	 reconfigure/0,

	 system_start_time/0,
	 sys_up_time/0,

	 info/0, 
	 verbosity/2 
	]).

-export([format_reason/1, format_reason/2]).

%% Backward compatibillity exports
-export([
	 agent_info/3, update_agent_info/5, 
	 g/3, g/4, g/5, g/6, g/7, 
	 ag/3, ag/4, ag/5, ag/6, ag/7, 
	 gn/3, gn/4, gn/5, gn/6, gn/7, 
	 agn/3, agn/4, agn/5, agn/6, agn/7, 
	 gb/5, gb/6, gb/7, gb/8, gb/9, 
	 agb/5, agb/6, agb/7, agb/8, agb/9, 
	 s/3, s/4, s/5, s/6, s/7, 
	 as/3, as/4, as/5, as/6, as/7
	]).

%% Application internal export
-export([start_link/3, snmpm_start_verify/2, snmpm_start_verify/3]).


-include("snmp_debug.hrl").
-include("snmpm_atl.hrl").
-include("snmp_types.hrl").

-define(DEFAULT_AGENT_PORT, 161).
-define(DEFAULT_CONTEXT,    "").


%% This function is called when the snmp application
%% starts. 
start_link(Opts, normal, []) ->
    start_link(Opts).


simple_conf() ->
    Vsns      = [v1, v2, v3],
    {ok, Cwd} = file:get_cwd(),
    %% Check if the manager config file exist, if not create it
    MgrConf = filename:join(Cwd, "manager.conf"),
    case file:read_file_info(MgrConf) of
	{ok, _} ->
	    ok;
	_ ->
	    ok = snmp_config:write_manager_config(Cwd, "", 
						  [{port, 5000},
						   {engine_id, "mgrEngine"},
						   {max_message_size, 484}])
    end,
    Conf = [{dir, Cwd}, {db_dir, Cwd}],
    [{versions, Vsns}, {config, Conf}].
    
%% Simple start. Start a manager with default values.
start_link() ->
    start_link(simple_conf()).

%% This function is normally not used. Instead the manager is
%% started as a consequence of a call to application:start(snmp)
%% when {snmp, [{manager, Options}]} is present in the
%% node config file.
start_link(Opts) ->
    %% This start the manager top supervisor, which in turn
    %% starts the other processes.
    {ok, _} = snmpm_supervisor:start_link(normal, Opts),
    ok.

%% Simple start. Start a manager with default values.
start() ->
    start(simple_conf()).
    
start(Opts) ->
    %% This start the manager top supervisor, which in turn
    %% starts the other processes.
    {ok, Pid} = snmpm_supervisor:start_link(normal, Opts),
    unlink(Pid),
    ok.

stop() ->
    snmpm_supervisor:stop().


monitor() ->
    erlang:monitor(process, snmpm_supervisor).

demonitor(Ref) ->
    erlang:demonitor(Ref).
	

-define(NOTIFY_START_TICK_TIME, 500).

notify_started(To) when is_integer(To) andalso (To > 0) ->
    spawn_link(?MODULE, snmpm_start_verify, [self(), To]).

cancel_notify_started(Pid) ->
    Pid ! {cancel, self()},
    ok.

snmpm_start_verify(Parent, To) ->
    ?d("starting", []),
    snmpm_start_verify(Parent, monitor(), To).

snmpm_start_verify(Parent, _Ref, To) when (To =< 0) ->
    ?d("timeout", []),
    unlink(Parent),
    Parent ! {snmpm_start_timeout, self()};
snmpm_start_verify(Parent, Ref, To) ->
    T0 = t(),
    receive
	{cancel, Parent} ->
	    ?d("cancel", []),
	    demonitor(Ref),
	    unlink(Parent),
	    exit(normal);
	{'EXIT', Parent, _} ->
	    exit(normal);
	{'DOWN', Ref, process, _Object, _Info} ->
	    ?d("down", []),
	    sleep(?NOTIFY_START_TICK_TIME),
	    ?MODULE:snmpm_start_verify(Parent, monitor(), t(T0, To))
    after ?NOTIFY_START_TICK_TIME ->
	    ?d("down timeout", []),
	    demonitor(Ref),
	    case snmpm_server:is_started() of
		true ->
		    unlink(Parent),
		    Parent ! {snmpm_started, self()};
		_ ->
		    ?MODULE:snmpm_start_verify(Parent, monitor(), t(T0, To))
	    end
    end.

t(T0, T)  -> T - (t() - T0).
t()       -> snmp_misc:now(ms).
sleep(To) -> snmp_misc:sleep(To).


%% -- Misc --

backup(BackupDir) ->
    snmpm_config:backup(BackupDir).


%% -- Mibs --

%% Load a mib into the manager
load_mib(MibFile) ->
    snmpm_server:load_mib(MibFile).

%% Unload a mib from the manager
unload_mib(Mib) ->
    snmpm_server:unload_mib(Mib).

%% Which mib's are loaded
which_mibs() ->
    snmpm_config:which_mibs().

%% Get all the possible oid's for the aliasname
name_to_oid(Name) ->
    snmpm_config:name_to_oid(Name).

%% Get the aliasname for an oid
oid_to_name(Oid) ->
    snmpm_config:oid_to_name(Oid).

%% Get the type for an oid
oid_to_type(Oid) ->
    snmpm_config:oid_to_type(Oid).


%% -- Info -- 

info() ->
    snmpm_server:info().


%% -- Verbosity -- 

%% Change the verbosity of a process in the manager
verbosity(config, V) ->
    snmpm_config:verbosity(V);
verbosity(server, V) ->
    snmpm_server:verbosity(V);
verbosity(net_if, V) ->
    snmpm_server:verbosity(net_if, V);
verbosity(note_store, V) ->
    snmpm_server:verbosity(note_store, V);
verbosity(all, V) ->
    snmpm_config:verbosity(V),
    snmpm_server:verbosity(V),
    snmpm_server:verbosity(net_if, V),
    snmpm_server:verbosity(note_store, V).


%% -- Users --

%% Register the 'user'. 
%% The manager entity responsible for a specific agent. 
%% Module is the callback module (snmpm_user behaviour) which 
%% will be called whenever something happens (detected 
%% agent, incomming reply or incomming trap/notification).
%% Note that this could have already been done as a 
%% consequence of the node config.
register_user(Id, Module, Data) ->
    register_user(Id, Module, Data, []).

%% Default config for agents registered by this user
register_user(Id, Module, Data, DefaultAgentConfig) ->
    snmpm_server:register_user(Id, Module, Data, DefaultAgentConfig).

register_user_monitor(Id, Module, Data) ->
    register_user_monitor(Id, Module, Data, []).

register_user_monitor(Id, Module, Data, DefaultAgentConfig) ->
    snmpm_server:register_user_monitor(Id, Module, Data, DefaultAgentConfig).

unregister_user(Id) ->
    snmpm_server:unregister_user(Id).

which_users() ->
    snmpm_config:which_users().


%% -- Agents --

%% Explicitly instruct the manager to handle this agent.
%% Called to instruct the manager that this agent 
%% shall be handled. These functions is used when
%% the user know's in advance which agents the
%% manager shall handle.
%% Note that there is an alternate way to do the same thing:
%% Add the agent to the manager config files.
%% 
%% UserId     -> Id of the user responsible for this agent: term()
%% TargetName -> Unique name for the agent: (string())
%% Config     -> Agent configuration: [config()]

do_register_agent(UserId, TargetName, Config) ->
    snmpm_config:register_agent(UserId, TargetName, Config).

register_agent(UserId, TargetName, Config) 
  when (is_list(TargetName) andalso 
	(length(TargetName) > 0) andalso 
	is_list(Config)) ->
    do_register_agent(UserId, TargetName, [{reg_type, target_name} | Config]);

%% Backward compatibillity 
%% Note that the agent engine id is a mandatory config option,
%% so this function *will* fail!
register_agent(UserId, Addr, Port) when is_integer(Port) ->
    register_agent(UserId, Addr, Port, []);

%% Backward compatibillity 
register_agent(UserId, Addr, Config) when is_list(Config) ->
    register_agent(UserId, Addr, ?DEFAULT_AGENT_PORT, Config).

%% Backward compatibillity 
%% Note that the agent engine id is a mandatory config option,
%% so this function *will* fail!
register_agent(UserId, Addr) ->
    register_agent(UserId, Addr, ?DEFAULT_AGENT_PORT, []).

%% Backward compatibillity 
register_agent(UserId, Addr, Port, Config0) ->
    case lists:keymember(target_name, 1, Config0) of
	false ->
	    TargetName = mk_target_name(Addr, Port, Config0), 
	    Config     = [{reg_type, addr_port}, 
			  {address, Addr}, {port, Port} | Config0], 
	    do_register_agent(UserId, TargetName, ensure_engine_id(Config));
	true ->
	    {value, {_, TargetName}} = 
		lists:keysearch(target_name, 1, Config0),
	    Config1 = lists:keydelete(target_name, 1, Config0),
	    Config2 = [{reg_type, addr_port}, 
		       {address, Addr}, {port, Port} | Config1], 
	    register_agent(UserId, TargetName, ensure_engine_id(Config2))
    end.

unregister_agent(UserId, TargetName) when is_list(TargetName) ->
    snmpm_config:unregister_agent(UserId, TargetName);

%% Backward compatibillity functions
unregister_agent(UserId, Addr) ->
    unregister_agent(UserId, Addr, ?DEFAULT_AGENT_PORT).

unregister_agent(UserId, Addr, Port) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    unregister_agent(UserId, TargetName);
	Error ->
	    Error
    end.

agent_info(TargetName, Item) ->
    snmpm_config:agent_info(TargetName, Item).

%% Backward compatibillity
agent_info(Addr, Port, Item) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    agent_info(TargetName, Item);
	Error ->
	    Error
    end.

update_agent_info(UserId, TargetName, Item, Val) ->
%%     p("update_agent_info -> entry with"
%%       "~n   UserId:     ~p"
%%       "~n   TargetName: ~p"
%%       "~n   Item:       ~p"
%%       "~n   Val:        ~p", [UserId, TargetName, Item, Val]),
    snmpm_config:update_agent_info(UserId, TargetName, Item, Val).

%% Backward compatibillity functions
update_agent_info(UserId, Addr, Port, Item, Val) ->
%%     p("update_agent_info -> entry with"
%%       "~n   UserId: ~p"
%%       "~n   Addr:   ~p"
%%       "~n   Port:   ~p"
%%       "~n   Item:   ~p"
%%       "~n   Val:    ~p", [UserId, Addr, Port, Item, Val]),
    case target_name(Addr, Port) of
	{ok, TargetName} ->
%% 	    p("update_agent_info -> TargetName: ~p", [TargetName]),
	    update_agent_info(UserId, TargetName, Item, Val);
	Error ->
	    Error
    end.

which_agents() ->
    snmpm_config:which_agents().

which_agents(UserId) ->
    snmpm_config:which_agents(UserId).


%% -- USM users --

register_usm_user(EngineID, UserName, Conf) 
  when is_list(EngineID) andalso is_list(UserName) andalso is_list(Conf) ->
    snmpm_config:register_usm_user(EngineID, UserName, Conf).

unregister_usm_user(EngineID, UserName) 
  when is_list(EngineID) andalso is_list(UserName) ->
    snmpm_config:unregister_usm_user(EngineID, UserName).

usm_user_info(EngineID, UserName, Item) 
  when is_list(EngineID) andalso is_list(UserName) andalso is_atom(Item) ->
    snmpm_config:usm_user_info(EngineID, UserName, Item).

update_usm_user_info(EngineID, UserName, Item, Val) 
  when is_list(EngineID) andalso is_list(UserName) andalso is_atom(Item) ->
    snmpm_config:update_usm_user_info(EngineID, UserName, Item, Val).

which_usm_users() ->
    snmpm_config:which_usm_users().

which_usm_users(EngineID) when is_list(EngineID) ->
    snmpm_config:which_usm_users(EngineID).


%% -- Discovery --

%% Start a discovery process
%% discovery(UserId, BAddr) ->
%%     snmpm_server:discovery(UserId, BAddr).

%% discovery(UserId, BAddr, ExpireOrConfig) ->
%%     snmpm_server:discovery(UserId, BAddr, ExpireOrConfig).

%% discovery(UserId, BAddr, Config, Expire) ->
%%     snmpm_server:discovery(UserId, BAddr, Config, Expire).

%% discovery(UserId, BAddr, Port, Config, Expire) ->
%%     snmpm_server:discovery(UserId, BAddr, Port, Config, Expire).

%% discovery(UserId, BAddr, Port, Config, Expire, ExtraInfo) ->
%%     snmpm_server:discovery(UserId, BAddr, Port, Config, Expire, ExtraInfo).


%% -- Requests --

%% --- synchroneous get-request ---
%% 

sync_get(UserId, TargetName, Oids) ->
    sync_get(UserId, TargetName, ?DEFAULT_CONTEXT, Oids).

sync_get(UserId, TargetName, {community, Community}, Oids) 
  when is_list(Oids) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_get(UserId, TargetName, CC, Oids);
sync_get(UserId, TargetName, CC, Oids) when is_list(Oids) ->
    snmpm_server:sync_get(UserId, TargetName, CC, Oids);
sync_get(UserId, TargetName, Oids, Timeout) when is_integer(Timeout) ->
    sync_get(UserId, TargetName, ?DEFAULT_CONTEXT, Oids, Timeout).

sync_get(UserId, TargetName, {community, Community}, Oids, Timeout) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_get(UserId, TargetName, CC, Oids, Timeout);
sync_get(UserId, TargetName, CC, Oids, Timeout) ->
    snmpm_server:sync_get(UserId, TargetName, CC, Oids, Timeout).

sync_get(UserId, TargetName, {community, Community}, 
	 Oids, Timeout, ExtraInfo) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_get(UserId, TargetName, CC, Oids, Timeout, ExtraInfo);
sync_get(UserId, TargetName, CC, Oids, Timeout, ExtraInfo) ->
    snmpm_server:sync_get(UserId, TargetName, CC, Oids, Timeout, ExtraInfo).


g(UserId, Addr, Oids) ->
    g(UserId, Addr, ?DEFAULT_AGENT_PORT, Oids).

g(UserId, Addr, Port, Oids) when is_integer(Port) andalso is_list(Oids) ->
    g(UserId, Addr, Port, ?DEFAULT_CONTEXT, Oids);
g(UserId, Addr, CC, Oids) when is_list(Oids) ->
    g(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, Oids);
g(UserId, Addr, Oids, Timeout) when is_integer(Timeout) ->
    g(UserId, Addr, ?DEFAULT_AGENT_PORT, Oids, Timeout).

g(UserId, Addr, Port, CC, Oids) 
  when is_integer(Port) andalso is_list(Oids) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_get(UserId, TargetName, CC, Oids);
	Error ->
	    Error
    end;    
    
g(UserId, Addr, Port, Oids, Timeout) 
  when is_integer(Port) andalso is_list(Oids) andalso is_integer(Timeout) ->
    g(UserId, Addr, Port, ?DEFAULT_CONTEXT, Oids, Timeout);

g(UserId, Addr, CC, Oids, Timeout) 
  when is_list(Oids) andalso is_integer(Timeout) ->
    g(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, Oids, Timeout).

g(UserId, Addr, Port, CC, Oids, Timeout) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_get(UserId, TargetName, CC, Oids, Timeout);
	Error ->
	    Error
    end.

g(UserId, Addr, Port, CC, Oids, Timeout, ExtraInfo) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_get(UserId, TargetName, CC, Oids, Timeout, ExtraInfo);
	Error ->
	    Error
    end.



%% --- asynchroneous get-request ---
%% 
%% The reply will be delivered to the user
%% through a call to handle_pdu/5
%% 

async_get(UserId, TargetName, Oids) ->
    async_get(UserId, TargetName, ?DEFAULT_CONTEXT, Oids).

async_get(UserId, TargetName, {community, Community}, Oids) 
  when is_list(Oids) ->
    CC = {?DEFAULT_CONTEXT, Community},     
    snmpm_server:async_get(UserId, TargetName, CC, Oids);
async_get(UserId, TargetName, CC, Oids) when is_list(Oids) ->
    snmpm_server:async_get(UserId, TargetName, CC, Oids);
async_get(UserId, TargetName, Oids, Expire) when is_integer(Expire) ->
    async_get(UserId, TargetName, ?DEFAULT_CONTEXT, Oids, Expire).

async_get(UserId, TargetName, {community, Community}, Oids, Expire) ->
    CC = {?DEFAULT_CONTEXT, Community},     
    snmpm_server:async_get(UserId, TargetName, CC, Oids, Expire);
async_get(UserId, TargetName, CC, Oids, Expire) ->
    snmpm_server:async_get(UserId, TargetName, CC, Oids, Expire).

async_get(UserId, TargetName, {community, Community}, 
	  Oids, Expire, ExtraInfo) ->
    CC = {?DEFAULT_CONTEXT, Community},     
    snmpm_server:async_get(UserId, TargetName, CC, Oids, Expire, ExtraInfo);
async_get(UserId, TargetName, CC, Oids, Expire, ExtraInfo) ->
    snmpm_server:async_get(UserId, TargetName, CC, Oids, Expire, ExtraInfo).


ag(UserId, Addr, Oids) ->
    ag(UserId, Addr, ?DEFAULT_AGENT_PORT, Oids).

ag(UserId, Addr, Port, Oids) when is_integer(Port) andalso is_list(Oids) ->
    ag(UserId, Addr, Port, ?DEFAULT_CONTEXT, Oids);
ag(UserId, Addr, Oids, Expire) when is_list(Oids) andalso is_integer(Expire) ->
    ag(UserId, Addr, ?DEFAULT_AGENT_PORT, ?DEFAULT_CONTEXT, Oids, Expire);
ag(UserId, Addr, CC, Oids) when is_list(Oids) ->
    ag(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, Oids).


ag(UserId, Addr, Port, CC, Oids) 
  when is_integer(Port) andalso is_list(Oids) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_get(UserId, TargetName, CC, Oids);
	Error ->
	    Error
    end;

ag(UserId, Addr, Port, Oids, Expire) 
  when is_integer(Port) andalso is_list(Oids) andalso is_integer(Expire) ->
    ag(UserId, Addr, Port, ?DEFAULT_CONTEXT, Oids, Expire);
ag(UserId, Addr, CC, Oids, Expire) 
  when is_list(Oids) andalso is_integer(Expire) ->
    ag(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, Oids, Expire).

ag(UserId, Addr, Port, CC, Oids, Expire) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_get(UserId, TargetName, CC, Oids, Expire);
	Error ->
	    Error
    end.

ag(UserId, Addr, Port, CC, Oids, Expire, ExtraInfo) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_get(UserId, TargetName, CC, Oids, Expire, ExtraInfo);
	Error ->
	    Error
    end.



%% --- synchroneous get_next-request ---
%% 

sync_get_next(UserId, TargetName, Oids) ->
    sync_get_next(UserId, TargetName, ?DEFAULT_CONTEXT, Oids).

sync_get_next(UserId, TargetName, {community, Community}, Oids) 
  when is_list(Oids) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_get_next(UserId, TargetName, CC, Oids);
sync_get_next(UserId, TargetName, CC, Oids) 
  when is_list(Oids) ->
    snmpm_server:sync_get_next(UserId, TargetName, CC, Oids);
sync_get_next(UserId, TargetName, Oids, Timeout) 
  when is_integer(Timeout) ->
    sync_get_next(UserId, TargetName, ?DEFAULT_CONTEXT, Oids, Timeout).


sync_get_next(UserId, TargetName, {community, Community}, Oids, Timeout) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_get_next(UserId, TargetName, CC, Oids, Timeout);
sync_get_next(UserId, TargetName, CC, Oids, Timeout) ->
    snmpm_server:sync_get_next(UserId, TargetName, CC, Oids, Timeout).

sync_get_next(UserId, TargetName, {community, Community}, 
	      Oids, Timeout, ExtraInfo) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_get_next(UserId, TargetName, CC, Oids, Timeout, 
			       ExtraInfo);
sync_get_next(UserId, TargetName, CC, Oids, Timeout, ExtraInfo) ->
    snmpm_server:sync_get_next(UserId, TargetName, CC, Oids, Timeout, 
			       ExtraInfo).


gn(UserId, Addr, Oids) ->
    gn(UserId, Addr, ?DEFAULT_AGENT_PORT, Oids).

gn(UserId, Addr, Oids, Timeout) 
  when is_list(Oids) andalso is_integer(Timeout) ->
    gn(UserId, Addr, ?DEFAULT_AGENT_PORT, Oids, Timeout);
gn(UserId, Addr, Port, Oids) 
  when is_integer(Port) andalso is_list(Oids) ->
    gn(UserId, Addr, Port, ?DEFAULT_CONTEXT, Oids);
gn(UserId, Addr, CC, Oids) 
  when is_list(Oids) ->
    gn(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, Oids).

gn(UserId, Addr, Port, Oids, Timeout) 
  when is_integer(Port) andalso is_list(Oids) andalso is_integer(Timeout) ->
    gn(UserId, Addr, Port, ?DEFAULT_CONTEXT, Oids, Timeout);
gn(UserId, Addr, Port, CC, Oids) 
  when is_integer(Port) andalso is_list(Oids) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_get_next(UserId, TargetName, CC, Oids);
	Error ->
	    Error
    end;    
gn(UserId, Addr, CC, Oids, Timeout) 
  when is_list(Oids) andalso is_integer(Timeout) ->
    gn(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, Oids, Timeout).

gn(UserId, Addr, Port, CC, Oids, Timeout) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_get_next(UserId, TargetName, CC, Oids, Timeout);
	Error ->
	    Error
    end.

gn(UserId, Addr, Port, CC, Oids, Timeout, ExtraInfo) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_get_next(UserId, TargetName, CC, Oids, Timeout, ExtraInfo);
	Error ->
	    Error
    end.



%% --- asynchroneous get_next-request ---
%% 

async_get_next(UserId, TargetName, Oids) ->
    async_get_next(UserId, TargetName, ?DEFAULT_CONTEXT, Oids).

async_get_next(UserId, TargetName, {community, Community}, Oids) 
  when is_list(Oids) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:async_get_next(UserId, TargetName, CC, Oids);
async_get_next(UserId, TargetName, CC, Oids) 
  when is_list(Oids) ->
    snmpm_server:async_get_next(UserId, TargetName, CC, Oids);
async_get_next(UserId, TargetName, Oids, Timeout) 
  when is_integer(Timeout) ->
    async_get_next(UserId, TargetName, ?DEFAULT_CONTEXT, Oids, Timeout).

async_get_next(UserId, TargetName, {community, Community}, Oids, Timeout) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:async_get_next(UserId, TargetName, CC, Oids, Timeout);
async_get_next(UserId, TargetName, CC, Oids, Timeout) ->
    snmpm_server:async_get_next(UserId, TargetName, CC, Oids, Timeout).

async_get_next(UserId, TargetName, {community, Community}, 
	       Oids, Timeout, ExtraInfo) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:async_get_next(UserId, TargetName, CC, Oids, Timeout, 
				ExtraInfo);
async_get_next(UserId, TargetName, CC, Oids, Timeout, ExtraInfo) ->
    snmpm_server:async_get_next(UserId, TargetName, CC, Oids, Timeout, 
				ExtraInfo).

agn(UserId, Addr, Oids) ->
    agn(UserId, Addr, ?DEFAULT_AGENT_PORT, Oids).

agn(UserId, Addr, Oids, Expire) 
  when is_list(Oids) andalso is_integer(Expire) ->
    agn(UserId, Addr, ?DEFAULT_AGENT_PORT, Oids, Expire);
agn(UserId, Addr, Port, Oids) 
  when is_integer(Port) andalso is_list(Oids) ->
    agn(UserId, Addr, Port, ?DEFAULT_CONTEXT, Oids);
agn(UserId, Addr, CC, Oids) 
  when is_list(Oids) ->
    agn(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, Oids).

agn(UserId, Addr, Port, Oids, Expire) 
  when is_integer(Port) andalso is_integer(Expire) ->
    agn(UserId, Addr, Port, ?DEFAULT_CONTEXT, Oids, Expire);
agn(UserId, Addr, CC, Oids, Expire) 
  when is_integer(Expire) ->
    agn(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, Oids, Expire);
agn(UserId, Addr, Port, CC, Oids) 
  when is_integer(Port) andalso is_list(Oids) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_get_next(UserId, TargetName, CC, Oids);
	Error ->
	    Error
    end. 

agn(UserId, Addr, Port, CC, Oids, Expire) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_get_next(UserId, TargetName, CC, Oids, Expire);
	Error ->
	    Error
    end.

agn(UserId, Addr, Port, CC, Oids, Expire, ExtraInfo) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_get_next(UserId, TargetName, CC, Oids, Expire, ExtraInfo);
	Error ->
	    Error
    end.



%% --- synchroneous set-request ---
%% 

sync_set(UserId, TargetName, VarsAndVals) ->
    sync_set(UserId, TargetName, ?DEFAULT_CONTEXT, VarsAndVals).

sync_set(UserId, TargetName, VarsAndVals, Timeout) 
  when is_list(VarsAndVals) andalso is_integer(Timeout) ->
    sync_set(UserId, TargetName, ?DEFAULT_CONTEXT, VarsAndVals, Timeout);
sync_set(UserId, TargetName, {community, Community}, VarsAndVals) 
  when is_list(VarsAndVals) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_set(UserId, TargetName, CC, VarsAndVals);
sync_set(UserId, TargetName, CC, VarsAndVals) 
  when is_list(VarsAndVals) ->
    snmpm_server:sync_set(UserId, TargetName, CC, VarsAndVals).

sync_set(UserId, TargetName, {community, Community}, VarsAndVals, Timeout) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_set(UserId, TargetName, CC, VarsAndVals, Timeout);
sync_set(UserId, TargetName, CC, VarsAndVals, Timeout) ->
    snmpm_server:sync_set(UserId, TargetName, CC, VarsAndVals, Timeout).

sync_set(UserId, TargetName, {community, Community}, 
	 VarsAndVals, Timeout, ExtraInfo) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_set(UserId, TargetName, CC, VarsAndVals, Timeout, 
			  ExtraInfo);
sync_set(UserId, TargetName, CC, VarsAndVals, Timeout, ExtraInfo) ->
    snmpm_server:sync_set(UserId, TargetName, CC, VarsAndVals, Timeout, 
			  ExtraInfo).


s(UserId, Addr, VarsAndVals) ->
    s(UserId, Addr, ?DEFAULT_AGENT_PORT, VarsAndVals).

s(UserId, Addr, Port, VarsAndVals) 
  when is_integer(Port) andalso is_list(VarsAndVals) ->
    s(UserId, Addr, Port, ?DEFAULT_CONTEXT, VarsAndVals);
s(UserId, Addr, VarsAndVals, Timeout) 
  when is_list(VarsAndVals) andalso is_integer(Timeout) ->
    s(UserId, Addr, ?DEFAULT_AGENT_PORT, VarsAndVals, Timeout);
s(UserId, Addr, CC, VarsAndVals) 
  when is_list(VarsAndVals) ->
    s(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, VarsAndVals).


s(UserId, Addr, Port, VarsAndVals, Timeout) 
  when is_integer(Port) andalso 
       is_list(VarsAndVals) andalso 
       is_integer(Timeout) ->
    s(UserId, Addr, Port, ?DEFAULT_CONTEXT, VarsAndVals, Timeout);
s(UserId, Addr, Port, CC, VarsAndVals) 
  when is_integer(Port) andalso is_list(VarsAndVals) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_set(UserId, TargetName, CC, VarsAndVals);
	Error ->
	    Error
    end;
s(UserId, Addr, CC, VarsAndVals, Timeout) 
  when is_list(VarsAndVals) andalso is_integer(Timeout) ->
    s(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, VarsAndVals, Timeout).

s(UserId, Addr, Port, CC, VarsAndVals, Timeout) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_set(UserId, TargetName, CC, VarsAndVals, Timeout);
	Error ->
	    Error
    end.

s(UserId, Addr, Port, CC, VarsAndVals, Timeout, ExtraInfo) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_set(UserId, TargetName, CC, VarsAndVals, Timeout, ExtraInfo);
	Error ->
	    Error
    end.



%% --- asynchroneous set-request --- 
%% 

async_set(UserId, TargetName, VarsAndVals) ->
    async_set(UserId, TargetName, ?DEFAULT_CONTEXT, VarsAndVals).

async_set(UserId, TargetName, VarsAndVals, Expire) 
  when is_list(VarsAndVals) andalso is_integer(Expire) ->
    async_set(UserId, TargetName, ?DEFAULT_CONTEXT, VarsAndVals, Expire);
async_set(UserId, TargetName, {community, Community}, VarsAndVals) 
  when is_list(VarsAndVals) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:async_set(UserId, TargetName, CC, VarsAndVals);
async_set(UserId, TargetName, CC, VarsAndVals) 
  when is_list(VarsAndVals) ->
    snmpm_server:async_set(UserId, TargetName, CC, VarsAndVals).

async_set(UserId, TargetName, {community, Community}, VarsAndVals, Expire) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:async_set(UserId, TargetName, CC, VarsAndVals, Expire);
async_set(UserId, TargetName, CC, VarsAndVals, Expire) ->
    snmpm_server:async_set(UserId, TargetName, CC, VarsAndVals, Expire).

async_set(UserId, TargetName, {community, Community}, 
	  VarsAndVals, Expire, ExtraInfo) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:async_set(UserId, TargetName, CC, VarsAndVals, Expire, 
			   ExtraInfo);
async_set(UserId, TargetName, CC, VarsAndVals, Expire, ExtraInfo) ->
    snmpm_server:async_set(UserId, TargetName, CC, VarsAndVals, Expire, 
			   ExtraInfo).


as(UserId, Addr, VarsAndVals) ->
    as(UserId, Addr, ?DEFAULT_AGENT_PORT, VarsAndVals).

as(UserId, Addr, VarsAndVals, Expire) 
  when is_list(VarsAndVals) andalso is_integer(Expire) ->
    as(UserId, Addr, ?DEFAULT_AGENT_PORT, VarsAndVals, Expire);
as(UserId, Addr, Port, VarsAndVals) 
  when is_integer(Port) andalso is_list(VarsAndVals) ->
    as(UserId, Addr, Port, ?DEFAULT_CONTEXT, VarsAndVals);
as(UserId, Addr, CC, VarsAndVals) 
  when is_list(VarsAndVals) ->
    as(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, VarsAndVals).


as(UserId, Addr, Port, VarsAndVals, Expire) 
  when is_integer(Port) andalso 
       is_list(VarsAndVals) andalso 
       is_integer(Expire) ->
    as(UserId, Addr, Port, ?DEFAULT_CONTEXT, VarsAndVals, Expire);
as(UserId, Addr, Port, CC, VarsAndVals) 
  when is_integer(Port) andalso is_list(VarsAndVals) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_set(UserId, TargetName, CC, VarsAndVals);
	Error ->
	    Error
    end;
as(UserId, Addr, CC, VarsAndVals, Expire) 
  when is_list(VarsAndVals) andalso is_integer(Expire) ->
    as(UserId, Addr, ?DEFAULT_AGENT_PORT, CC, VarsAndVals, Expire).

as(UserId, Addr, Port, CC, VarsAndVals, Expire) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_set(UserId, TargetName, CC, VarsAndVals, Expire);
	Error ->
	    Error
    end.

as(UserId, Addr, Port, CC, VarsAndVals, Expire, ExtraInfo) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_set(UserId, TargetName, CC, VarsAndVals, Expire, ExtraInfo);
	Error ->
	    Error
    end.




%% --- synchroneous get-bulk ---
%% 

sync_get_bulk(UserId, TargetName, NonRep, MaxRep, Oids) ->
    sync_get_bulk(UserId, TargetName, NonRep, MaxRep, ?DEFAULT_CONTEXT, Oids).

sync_get_bulk(UserId, TargetName, NonRep, MaxRep, Oids, Timeout) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) andalso 
       is_integer(Timeout) ->
    sync_get_bulk(UserId, TargetName, NonRep, MaxRep, 
		  ?DEFAULT_CONTEXT, Oids, Timeout);
sync_get_bulk(UserId, TargetName, 
	      NonRep, MaxRep, {community, Community}, Oids) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_get_bulk(UserId, TargetName, 
			       NonRep, MaxRep, 
			       CC, Oids);
sync_get_bulk(UserId, TargetName, NonRep, MaxRep, CC, Oids) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) ->
    snmpm_server:sync_get_bulk(UserId, TargetName, 
			       NonRep, MaxRep, 
			       CC, Oids).

sync_get_bulk(UserId, TargetName, 
	      NonRep, MaxRep, {community, Community}, Oids, Timeout) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_get_bulk(UserId, TargetName, 
			       NonRep, MaxRep, CC, Oids, Timeout);
sync_get_bulk(UserId, TargetName, NonRep, MaxRep, CC, Oids, Timeout) ->
    snmpm_server:sync_get_bulk(UserId, TargetName, 
			       NonRep, MaxRep, CC, Oids, Timeout).

sync_get_bulk(UserId, TargetName, 
	      NonRep, MaxRep, {community, Community}, 
	      Oids, Timeout, ExtraInfo) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:sync_get_bulk(UserId, TargetName, 
			       NonRep, MaxRep, CC, Oids, Timeout, ExtraInfo);
sync_get_bulk(UserId, TargetName, NonRep, MaxRep, CC, Oids, Timeout, 
	      ExtraInfo) ->
    snmpm_server:sync_get_bulk(UserId, TargetName, 
			       NonRep, MaxRep, CC, Oids, Timeout, ExtraInfo).


gb(UserId, Addr, NonRep, MaxRep, Oids) ->
    gb(UserId, Addr, ?DEFAULT_AGENT_PORT, NonRep, MaxRep, Oids).

gb(UserId, Addr, NonRep, MaxRep, Oids, Timeout) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) andalso 
       is_integer(Timeout) ->
    gb(UserId, Addr, ?DEFAULT_AGENT_PORT, NonRep, MaxRep, Oids, Timeout);
gb(UserId, Addr, Port, NonRep, MaxRep, Oids) 
  when is_integer(Port) andalso 
       is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) ->
    gb(UserId, Addr, Port, NonRep, MaxRep, ?DEFAULT_CONTEXT, Oids);
gb(UserId, Addr, NonRep, MaxRep, CC, Oids) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) ->
    gb(UserId, Addr, ?DEFAULT_AGENT_PORT, NonRep, MaxRep, CC, Oids).

gb(UserId, Addr, Port, NonRep, MaxRep, Oids, Timeout) 
  when is_integer(Port) andalso 
       is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) andalso 
       is_integer(Timeout) ->
    gb(UserId, Addr, Port, NonRep, MaxRep, ?DEFAULT_CONTEXT, Oids, Timeout);
gb(UserId, Addr, NonRep, MaxRep, CC, Oids, Timeout) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) andalso 
       is_integer(Timeout) ->
    gb(UserId, Addr, ?DEFAULT_AGENT_PORT, NonRep, MaxRep, CC, Oids, Timeout);
gb(UserId, Addr, Port, NonRep, MaxRep, CC, Oids) 
  when is_integer(Port) andalso 
       is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_get_bulk(UserId, TargetName, NonRep, MaxRep, CC, Oids);
	Error ->
	    Error
    end.

gb(UserId, Addr, Port, NonRep, MaxRep, CC, Oids, Timeout) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_get_bulk(UserId, TargetName, 
			  NonRep, MaxRep, CC, Oids, Timeout);
	Error ->
	    Error
    end.

gb(UserId, Addr, Port, NonRep, MaxRep, CC, Oids, Timeout, ExtraInfo) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    sync_get_bulk(UserId, TargetName, 
			  NonRep, MaxRep, CC, Oids, Timeout, ExtraInfo);
	Error ->
	    Error
    end.



%% --- asynchroneous get-bulk ---
%% 

async_get_bulk(UserId, TargetName, NonRep, MaxRep, Oids) ->
    async_get_bulk(UserId, TargetName, NonRep, MaxRep, ?DEFAULT_CONTEXT, Oids).

async_get_bulk(UserId, TargetName, NonRep, MaxRep, Oids, Expire) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) andalso 
       is_integer(Expire) ->
    async_get_bulk(UserId, TargetName, 
		   NonRep, MaxRep, ?DEFAULT_CONTEXT, Oids, Expire);
async_get_bulk(UserId, TargetName, 
	       NonRep, MaxRep, {community, Community}, Oids) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:async_get_bulk(UserId, TargetName, 
				NonRep, MaxRep, CC, Oids);
async_get_bulk(UserId, TargetName, NonRep, MaxRep, CC, Oids) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) ->
    snmpm_server:async_get_bulk(UserId, TargetName, 
				NonRep, MaxRep, CC, Oids).

async_get_bulk(UserId, TargetName, NonRep, MaxRep, {community, Community}, 
	       Oids, Expire) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:async_get_bulk(UserId, TargetName, 
				NonRep, MaxRep, CC, Oids, Expire);
async_get_bulk(UserId, TargetName, NonRep, MaxRep, CC, Oids, Expire) ->
    snmpm_server:async_get_bulk(UserId, TargetName, 
				NonRep, MaxRep, CC, Oids, Expire).

async_get_bulk(UserId, TargetName, 
	       NonRep, MaxRep, {community, Community}, 
	       Oids, Expire, ExtraInfo) ->
    CC = {?DEFAULT_CONTEXT, Community}, 
    snmpm_server:async_get_bulk(UserId, TargetName, 
				NonRep, MaxRep, CC, Oids, Expire, ExtraInfo);
async_get_bulk(UserId, TargetName, NonRep, MaxRep, CC, Oids, Expire, 
	       ExtraInfo) ->
    snmpm_server:async_get_bulk(UserId, TargetName, 
				NonRep, MaxRep, CC, Oids, Expire, ExtraInfo).


agb(UserId, Addr, NonRep, MaxRep, Oids) ->
    agb(UserId, Addr, ?DEFAULT_AGENT_PORT, NonRep, MaxRep, Oids).

agb(UserId, Addr, NonRep, MaxRep, Oids, Expire) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) andalso 
       is_integer(Expire) ->
    agb(UserId, Addr, ?DEFAULT_AGENT_PORT, NonRep, MaxRep, Oids, Expire);
agb(UserId, Addr, Port, NonRep, MaxRep, Oids) 
  when is_integer(Port) andalso 
       is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) ->
    agb(UserId, Addr, Port, NonRep, MaxRep, ?DEFAULT_CONTEXT, Oids);
agb(UserId, Addr, NonRep, MaxRep, CC, Oids) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) ->
    agb(UserId, Addr, ?DEFAULT_AGENT_PORT, NonRep, MaxRep, CC, Oids).

agb(UserId, Addr, Port, NonRep, MaxRep, Oids, Expire) 
  when is_integer(Port) andalso 
       is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) andalso 
       is_integer(Expire) ->
    agb(UserId, Addr, Port, NonRep, MaxRep, ?DEFAULT_CONTEXT, Oids, Expire);
agb(UserId, Addr, NonRep, MaxRep, CC, Oids, Expire) 
  when is_integer(NonRep) andalso 
       is_integer(MaxRep) andalso 
       is_list(Oids) andalso 
       is_integer(Expire) ->
    agb(UserId, Addr, ?DEFAULT_AGENT_PORT, NonRep, MaxRep, CC, Oids);
agb(UserId, Addr, Port, NonRep, MaxRep, CC, Oids) 
  when is_integer(Port) andalso 
       is_integer(NonRep) andalso 
       is_integer(MaxRep), 
       is_list(Oids) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_get_bulk(UserId, TargetName, 
			   NonRep, MaxRep, CC, Oids);
	Error ->
	    Error
    end.

agb(UserId, Addr, Port, NonRep, MaxRep, CC, Oids, Expire) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_get_bulk(UserId, TargetName, 
			   NonRep, MaxRep, CC, Oids, Expire);
	Error ->
	    Error
    end.

agb(UserId, Addr, Port, NonRep, MaxRep, CC, Oids, Expire, ExtraInfo) ->
    case target_name(Addr, Port) of
	{ok, TargetName} ->
	    async_get_bulk(UserId, TargetName, 
			   NonRep, MaxRep, CC, Oids, Expire, ExtraInfo);
	Error ->
	    Error
    end.


cancel_async_request(UserId, ReqId) ->
    snmpm_server:cancel_async_request(UserId, ReqId).


%%%-----------------------------------------------------------------
%%% Audit Trail Log functions (for backward compatibillity)
%%%-----------------------------------------------------------------
log_to_txt(LogDir, Mibs) ->
    OutFile = "snmpm_log.txt",       
    LogName = ?audit_trail_log_name, 
    LogFile = ?audit_trail_log_file, 
    snmp:log_to_txt(LogDir, Mibs, OutFile, LogName, LogFile).
log_to_txt(LogDir, Mibs, OutFile) ->
    LogName = ?audit_trail_log_name, 
    LogFile = ?audit_trail_log_file, 
    snmp:log_to_txt(LogDir, Mibs, OutFile, LogName, LogFile).
log_to_txt(LogDir, Mibs, OutFile, LogName) ->
    LogFile = ?audit_trail_log_file, 
    snmp:log_to_txt(LogDir, Mibs, OutFile, LogName, LogFile).
log_to_txt(LogDir, Mibs, OutFile, LogName, LogFile) -> 
    snmp:log_to_txt(LogDir, Mibs, OutFile, LogName, LogFile).
log_to_txt(LogDir, Mibs, OutFile, LogName, LogFile, Start) -> 
    snmp:log_to_txt(LogDir, Mibs, OutFile, LogName, LogFile, Start).
log_to_txt(LogDir, Mibs, OutFile, LogName, LogFile, Start, Stop) -> 
    snmp:log_to_txt(LogDir, Mibs, OutFile, LogName, LogFile, Start, Stop).


change_log_size(NewSize) ->
    LogName = ?audit_trail_log_name, 
    snmp:change_log_size(LogName, NewSize).


get_log_type() ->
    snmpm_server:get_log_type().

%% NewType -> atl_type()
set_log_type(NewType) ->
    snmpm_server:set_log_type(NewType).


reconfigure() ->
    snmpm_server:reconfigure().


%%%-----------------------------------------------------------------

system_start_time() ->
    {ok, Time} = snmpm_config:system_start_time(),
    Time.

sys_up_time() ->
    % time in 0.01 seconds.
    StartTime = system_start_time(),
    (snmp_misc:now(cs) - StartTime) rem (2 bsl 31).


%%%-----------------------------------------------------------------
%%% This is just some simple utility functions to create a pretty-
%%% printable string of the error reason received from either:
%%% 
%%%    * If any of the sync/async get/get-next/set/get-bulk
%%%      returnes {error, Reason} 
%%%    * The Reason parameter in the handle_error user callback 
%%%      function
%%% 
%%%-----------------------------------------------------------------

format_reason(Reason) ->
    format_reason("", Reason).

format_reason(Prefix, Reason) when is_integer(Prefix) and (Prefix >= 0) ->
    format_reason(lists:duplicate(Prefix, $ ), Reason);
format_reason(Prefix, Reason) when is_list(Prefix) ->
    case (catch do_format_reason(Prefix, Reason)) of
	FL when is_list(FL) ->
	    FL;
	_ ->
	    %% Crap, try it without any fancy formatting
	    case (catch io_lib:format("~sInternal manager error: ~n"
				      "~s   ~p~n", 
				      [Prefix, Prefix, Reason])) of
		L1 when is_list(L1) ->
		    lists:flatten(L1);
		_ ->
		    %% Really crap, try it without the prefix
		    case (catch io_lib:format("Internal manager error: ~n"
					      "   ~p~n", 
					      [Reason])) of
			L2 when is_list(L2) ->
			    lists:flatten(L2);
			_ ->
			    %% Ok, I give up
			    "Illegal input. Unable to format error reason"
		    end
	    end
    end.
		    

do_format_reason(Prefix, {failed_generating_response, {RePdu, Reason}}) ->
    FmtPdu = format_pdu(Prefix ++ "   ", RePdu),
    lists:flatten(io_lib:format("~sFailed generating response: ~n"
				"~s"
				"~s   ~p~n", 
				[Prefix, FmtPdu, Prefix, Reason]));
do_format_reason(Prefix, {failed_processing_message, Reason})  ->
    lists:flatten(io_lib:format("~sFailed processing message: ~n"
				"~s   ~p~n", 
				[Prefix, Prefix, Reason]));
do_format_reason(Prefix, {unexpected_pdu, SnmpInfo})  ->
    FmtSnmpInfo = format_snmp_info(Prefix ++ "   ", SnmpInfo),
    lists:flatten(io_lib:format("~sUnexpected PDU: ~n~s", 
				[Prefix, FmtSnmpInfo]));
do_format_reason(Prefix, {send_failed, ReqId, Reason})  ->
    lists:flatten(io_lib:format("~sSend failed: ~n"
				"~s   Request id: ~w~n"
				"~s   Reason:     ~p~n", 
				[Prefix, Prefix, ReqId, Prefix, Reason]));
do_format_reason(Prefix, {invalid_sec_info, SecInfo, SnmpInfo})  ->
    FmtSecInfo  = format_sec_info(Prefix ++ "   ", SecInfo),
    FmtSnmpInfo = format_snmp_info(Prefix ++ "   ", SnmpInfo),
    lists:flatten(io_lib:format("~sInvalid security info: ~n"
				"~s"
				"~s", 
				[Prefix, FmtSecInfo, FmtSnmpInfo]));
do_format_reason(Prefix, Reason)  ->
    lists:flatten(io_lib:format("~sInternal manager error: ~n"
				"~s   ~p~n", [Prefix, Prefix, Reason])).

format_pdu(Prefix, #pdu{type         = Type,
			request_id   = ReqId,
			error_status = ES,
			error_index  = EI,
			varbinds     = VBs}) ->
    FmtPdyType   = format_pdu_type(Type),
    FmtErrStatus = format_error_status(ES),
    FmtErrIdx    = format_error_index(EI),
    FmtVBs       = format_varbinds(Prefix ++ "   ", VBs),
    lists:flatten(io_lib:format("~s~s: ~n"
				"~s   Request-id:   ~w~n"
				"~s   Error-status: ~s~n"
				"~s   Error-index:  ~s~n"
				"~s",
				[Prefix, FmtPdyType,
				 Prefix, ReqId, 
				 Prefix, FmtErrStatus, 
				 Prefix, FmtErrIdx, 
				 FmtVBs]));
format_pdu(Prefix, #trappdu{enterprise    = E,
			    agent_addr    = AA,
			    generic_trap  = GT,
			    specific_trap = ST,
			    time_stamp    = TS,
			    varbinds      = VBs}) ->
    FmtVBs = format_varbinds(Prefix ++ "   ", VBs),
    lists:flatten(io_lib:format("~sTrap PDU: ~n"
				"~s   Enterprise:    ~p~n"
				"~s   Agent address: ~p~n"
				"~s   Generic trap:  ~p~n"
				"~s   Specific trap: ~p~n"
				"~s   Time stamp:    ~p~n"
				"~s",
				[Prefix, 
				 Prefix, E,
				 Prefix, AA, 
				 Prefix, GT, 
				 Prefix, ST, 
				 Prefix, TS, 
				 FmtVBs]));
format_pdu(Prefix, PDU) ->
    lists:flatten(io_lib:format("~s~p~n", [Prefix, PDU])).

format_pdu_type('get-request') ->
    "GetRequest-PDU";
format_pdu_type('get-next-request') ->
    "GetNextRequest-PDU";
format_pdu_type('get-response') ->
    "Response-PDU";
format_pdu_type('set-request') ->
    "SetRequest-PDU";
format_pdu_type('get-bulk-request') ->
    "GetBulkRequest-PDU";
format_pdu_type('inform-request') ->
    "InformRequest-PDU";
format_pdu_type('snmpv2-trap') ->
    "SNMPv2-Trap-PDU";
format_pdu_type(report) ->
    "Report-PDU";
format_pdu_type(T) ->
    lists:flatten(io_lib:format("~p", [T])).
    
format_snmp_info(Prefix, {ES, EI, VBs}) ->
    lists:flatten(io_lib:format("~sSNMP info: ~n"
				"~s   Error-status: ~s~n"
				"~s   Error-index:  ~s~n"
				"~s",
				[Prefix, 
				 Prefix, format_error_status(ES),
				 Prefix, format_error_index(EI),
				 format_varbinds(Prefix ++ "   ", VBs)]));
format_snmp_info(Prefix, JunkSnmpInfo) ->
    lists:flatten(io_lib:format("~sJunk SNMP info: ~n"
				"~s   ~p~n",
				[Prefix, Prefix, JunkSnmpInfo])).

format_error_status(ES) ->
    lists:flatten(io_lib:format("~p", [ES])).

format_error_index(EI) ->
    lists:flatten(io_lib:format("~p", [EI])).

format_sec_info(Prefix, Info) ->
    FmtSecInfo = do_format_sec_info(Prefix ++ "   ", Info),
    lists:flatten(io_lib:format("~sSecurity info: ~n~s", 
				[Prefix, FmtSecInfo])).

do_format_sec_info(_Prefix, []) ->
    "";
do_format_sec_info(Prefix, [{Tag, ExpVal, Val}|T]) ->
    format_sec_info(Prefix, Tag, ExpVal, Val) ++
	do_format_sec_info(Prefix, T).


format_sec_info(_Prefix, _Tag, Val, Val) ->
    "";
format_sec_info(Prefix, Tag, ExpVal, Val) ->
    lists:flatten(io_lib:format("~s~s:~n"
				"~s   Expected value: ~p~n"
				"~s   Actual value:   ~p~n",
				[Prefix, format_sec_info_tag(Tag),
				 Prefix, ExpVal,
				 Prefix, Val])).

format_sec_info_tag(sec_engine_id) ->
    "Sec engine id";
format_sec_info_tag(msg_sec_model) ->
    "Msg sec model";
format_sec_info_tag(sec_name) ->
    "Sec name";
format_sec_info_tag(sec_level) ->
    "Sec level";
format_sec_info_tag(ctx_engine_id) ->
    "Context engine id";
format_sec_info_tag(ctx_name) ->
    "Context name";
format_sec_info_tag(request_id) ->
    "Request id";
format_sec_info_tag(T) ->
    lists:flatten(io_lib:format("~p", [T])).

format_varbinds(Prefix, []) ->
    lists:flatten(io_lib:format("~sVarbinds:    []~n", [Prefix])); 
format_varbinds(Prefix, VBs) when is_list(VBs) ->
    lists:flatten(io_lib:format("~sVarbinds: ~n~s", 
				[Prefix, format_vbs(Prefix ++ "   ", VBs)]));
format_varbinds(Prefix, VBs) ->
    lists:flatten(io_lib:format("~sInvalid varbinds: ~n"
				"~s   ~p~n", 
				[Prefix, Prefix, VBs])).

format_vbs(_Prefix, []) ->
    "";
format_vbs(Prefix, [VB|VBs]) ->
    format_vb(Prefix, VB) ++ format_vbs(Prefix, VBs).
    
format_vb(Prefix, #varbind{oid          = Oid0,
			   variabletype = Type,
			   value        = Val,
			   org_index    = Idx}) ->
    Oid = 
	case snmpm:oid_to_name(Oid0) of
	    {ok, O} ->
		O;
	    _ ->
		Oid0
	end,
    FmtVT  = format_vb_variabletype(Prefix ++ "   ", Type),
    FmtVal = format_vb_value(Prefix ++ "   ", Type, Val),
    lists:flatten(io_lib:format("~s~w:~n"
				"~s"
				"~s"
				"~s   Org-index:     ~p~n", 
				[Prefix, Oid, 
				 FmtVT, 
				 FmtVal, 
				 Prefix, Idx]));
format_vb(Prefix, JunkVB) ->
    lists:flatten(io_lib:format("~sJunk varbind:~n"
				"~s   ~p~n", [Prefix, Prefix, JunkVB])).

format_vb_variabletype(Prefix, Type) when is_atom(Type) ->
    lists:flatten(io_lib:format("~sVariable-type: ~s~n", 
				[Prefix, atom_to_list(Type)]));
format_vb_variabletype(Prefix, Type) ->
    lists:flatten(io_lib:format("~sVariable-type: ~p~n", [Prefix, Type])).

format_vb_value(Prefix, _Type, Val) ->
    lists:flatten(io_lib:format("~sValue:         ~p~n", [Prefix, Val])).


%% ---------------------------------------------------------------------------
%% 
%% --- Internal utility functions ---
%% 

target_name(Addr, Port) ->
    snmpm_config:agent_info(Addr, Port, target_name).

mk_target_name(Addr, Port, Config) ->
    snmpm_config:mk_target_name(Addr, Port, Config).

ensure_engine_id(Config) ->
    case lists:keymember(engine_id, 1, Config) of
	true ->
	    Config;
	false ->
	    DefaultEngineId = "agentEngine-default", 
	    [{engine_id, DefaultEngineId} | Config]
    end.



%% p(F) ->
%%     p(F, []).

%% p(F, A) ->
%%     io:format("~w:" ++ F ++ "~n", [?MODULE | A]).