-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
snmpm.erl
2402 lines (1991 loc) · 78 KB
/
snmpm.erl
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2024. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(snmpm).
-moduledoc """
Interface functions to the SNMP toolkit manager
The module `snmpm` contains interface functions to the SNMP manager.
""".
%%----------------------------------------------------------------------
%% This module implements a simple SNMP manager for Erlang.
%%----------------------------------------------------------------------
%% User interface
%% Avoid warning for local function demonitor/1 clashing with autoimported BIF.
-compile({no_auto_import,[demonitor/1]}).
-export([
%%
%% Management API
start/0, start/1,
start_link/0, start_link/1,
stop/0, stop/1,
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/3, 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 (version "3").
sync_get2/3, sync_get2/4,
async_get2/3, async_get2/4,
sync_get_next2/3, sync_get_next2/4,
async_get_next2/3, async_get_next2/4,
sync_set2/3, sync_set2/4,
async_set2/3, async_set2/4,
sync_get_bulk2/5, sync_get_bulk2/6,
async_get_bulk2/5, async_get_bulk2/6,
cancel_async_request/2,
%%
%% Extended SNMP API
%% discovery/2, discovery/3, discovery/4, discovery/5, discovery/6,
%%
%% Logging
log_to_txt/1, log_to_txt/2, log_to_txt/3, log_to_txt/4,
log_to_txt/5, log_to_txt/6, log_to_txt/7, log_to_txt/8,
log_to_io/1, log_to_io/2, log_to_io/3, log_to_io/4,
log_to_io/5, log_to_io/6, log_to_io/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, info/1,
verbosity/2,
restart/1
]).
-export([format_reason/1, format_reason/2]).
%% Application internal export
-export([start_link/3, snmpm_start_verify/2, snmpm_start_verify/3]).
-export([target_name/1, target_name/2]).
-export_type([
target_name/0,
user_id/0,
request_id/0,
register_timeout/0,
agent_config_item/0,
agent_config/0,
pdu_type/0,
value_type/0,
var_and_val/0,
snmpm_user/0,
usm_config_item/0,
snmp_reply/0
]).
-include_lib("snmp/src/misc/snmp_debug.hrl").
-include_lib("snmp/include/snmp_types.hrl").
-include("snmpm_atl.hrl").
-include("snmpm_internal.hrl").
-include("snmp_verbosity.hrl").
-define(DEFAULT_AGENT_PORT, 161).
-define(ATL_BLOCK_DEFAULT, true).
%%-----------------------------------------------------------------
%% Types
%%-----------------------------------------------------------------
-doc "Is a unique _non-empty_ string.".
-type target_name() :: string().
-doc "Is a unique term that identifies a user.".
-type user_id() :: term().
-doc "Is a unique term that identifies a request.".
-opaque request_id() :: term().
-doc "The time to complete a (agent) registration.".
-type register_timeout() :: pos_integer() | snmp:snmp_timer().
-doc """
Value type depend on the item according to:
- **`engine_id`** - Engine ID of the agent.
Value type: [engine_id()](`t:snmp:engine_id/0`)
- **`address`** - The IP address of the agent.
Value type: [ip_address()](`t:inet:ip_address/0`)
- **`port`** - Port number of the agent.
Value type: [port_number()](`t:inet:port_number/0`)
- **`tdomain`** - Transport domain.
Value type: [tdomain()](`t:snmp:tdomain/0`)
- **`community`** - Community.
Value type: [community()](`t:snmp:community/0`)
- **`timeout`** - Registration timeout.
Value type: `t:register_timeout/0`
- **`max_message_size`** - Max Message Size of a message.
Value type: [mms()](`t:snmp:mms/0`)
- **`version`** - What SNMP version is used when communicating with this agent.
Value type: [version()](`t:snmp:version/0`)
- **`sec_model`** - Security Model.
Value type: [sec_model()](`t:snmp:sec_model/0`)
- **`sec_name`** - Security Name.
Value type: [sec_name()](`t:snmp:sec_name/0`)
- **`sec_level`** - Security Level.
Value type: [sec_level()](`t:snmp:sec_level/0`)
""".
-type agent_config_item() :: engine_id |
address | port | tdomain |
community |
timeout |
max_message_size |
version |
sec_model | sec_name | sec_level.
-doc """
- **`o - 'OBJECT IDENTIFIER'`**
- **`i - 'INTEGER'`**
- **`u - 'Unsigned32`**
- **`g - 'Unsigned32'`**
- **`s - 'OCTET STRING'`**
- **`b - 'BITS'`**
- **`ip - 'IpAddress'`**
- **`op - 'Opaque'`**
- **`c32 - 'Counter32'`**
- **`c64 - 'Counter64'`**
- **`tt - 'TimeTicks'`**
""".
-type value_type() :: o |
i | u | g |
s | s | b |
ip | op |
c32 | c64 |
tt.
-type var_and_val() :: {OID :: snmp:oid(),
ValueType :: value_type(),
Value :: term()} |
{OID :: snmp:oid(),
Value :: term()}.
-type agent_config() ::
{engine_id, snmp:engine_id()} | % Mandatory
{address, inet:ip_address()} | % Mandatory
{port, inet:port_number()} | % Optional
{tdomain, snmp:tdomain()} | % Optional
{community, snmp:community()} | % Optional
{timeout, register_timeout()} | % Optional
{max_message_size, snmp:mms()} | % Optional
{version, snmp:version()} | % Optional
{sec_model, snmp:sec_model()} | % Optional
{sec_name, snmp:sec_name()} | % Optional
{sec_level, snmp:sec_level()}. % Optional
-type pdu_type() :: snmp:pdu_type() | 'trappdu'.
-doc "Module implementing the [snmpm_user](`m:snmpm_user#`) behaviour.".
-type snmpm_user() :: module().
-doc """
Value type depend on the item according to:
- **`sec_name`** - Security Name.
Value type: [`snmp:sec_name()`](`t:snmp:sec_name/0`)
- **`auth`** - Authentication protocol.
Value type: [`snmp:usm_auth_protocol()`](`t:snmp:usm_auth_protocol/0`)
- **`auth_key`** - Authentication key.
Value type: [`snmp:usm_auth_key()`](`t:snmp:usm_auth_key/0`)
- **`priv`** - Privacy protocol.
Value type: [`snmp:usm_priv_protocol()`](`t:snmp:usm_priv_protocol/0`)
- **`priv_key`** - Privacy key.
Value type: [`snmp:usm_priv_key()`](`t:snmp:usm_priv_key/0`)
""".
-type usm_config_item() :: sec_name | auth | auth_key | priv | priv_key.
-type snmp_reply() :: {snmp:error_status(),
snmp:error_index(),
[snmp:varbind()]}.
%% This function is called when the snmp application
%% starts.
-doc false.
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, "",
[snmpm_conf:manager_entry(port, 5000),
snmpm_conf:manager_entry(engine_id, "mgrEngine"),
snmpm_conf:manager_entry(max_message_size, 484)])
end,
Conf = [{dir, Cwd}, {db_dir, Cwd}],
[{versions, Vsns}, {config, Conf}].
%% Simple start. Start a manager with default values.
-doc false.
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.
-doc false.
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.
-doc false.
start() ->
start(simple_conf()).
-doc false.
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.
-doc false.
stop() ->
stop(0).
-doc false.
stop(Timeout) when (Timeout =:= infinity) orelse
(is_integer(Timeout) andalso (Timeout >= 0)) ->
snmpm_supervisor:stop(Timeout).
-doc """
Monitor the SNMP manager. In case of a crash, the calling (monitoring) process
will get a 'DOWN' message (see the erlang module for more info).
""".
-spec monitor() -> MRef when
MRef :: reference().
monitor() ->
erlang:monitor(process, snmpm_supervisor).
-doc """
Turn off monitoring of the SNMP manager.
""".
-spec demonitor(Ref) -> true when
Ref :: reference().
demonitor(Ref) ->
erlang:demonitor(Ref).
-define(NOTIFY_START_TICK_TIME, 500).
-doc """
Request a notification (message) when the SNMP manager has started.
The `Timeout` is the time the request is valid. The value has to be greater then
zero.
The `Pid` is the process handling the supervision of the SNMP manager start.
When the manager has started a completion message will be sent to the client
from this process: `{snmpm_started, Pid}`. If the SNMP manager was not started
in time, a timeout message will be sent to the client:
`{snmpm_start_timeout, Pid}`.
A client application that is dependent on the SNMP manager will use this
function in order to be notified of when the manager has started. There are two
situations when this is useful:
- During the start of a system, when a client application _could_ start prior to
the SNMP manager but is dependent upon it, and therefore has to wait for it to
start.
- When the SNMP manager has crashed, the dependent client application has to
wait for the SNMP manager to be restarted before it can _reconnect_.
The function returns the pid() of a handler process, that does the supervision
on behalf of the client application. Note that the client application is linked
to this handler.
This function is used in conjunction with the monitor function.
""".
-spec notify_started(Timeout) -> Pid when
Timeout :: pos_integer(),
Pid :: pid().
notify_started(To) when is_integer(To) andalso (To > 0) ->
spawn_link(?MODULE, snmpm_start_verify, [self(), To]).
-doc """
Cancel a previous request to be notified of SNMP manager start.
""".
-spec cancel_notify_started(Pid) -> snmp:void() when
Pid :: pid().
cancel_notify_started(Pid) ->
Pid ! {cancel, self()},
ok.
-doc false.
snmpm_start_verify(Parent, To) ->
?d("starting", []),
snmpm_start_verify(Parent, monitor(), To).
-doc false.
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 --
-doc """
Backup persistent data handled by the manager.
BackupDir cannot be identical to DbDir.
""".
-spec backup(BackupDir) -> ok | {error, Reason} when
BackupDir :: snmp:dir(),
Reason :: term().
backup(BackupDir) ->
snmpm_config:backup(BackupDir).
%% -- Mibs --
%% Load a mib into the manager
-doc """
Load a `Mib` into the manager. The `MibName` is the name of the Mib, including
the path to where the compiled mib is found. For example,
```erlang
Dir = code:priv_dir(my_app) ++ "/mibs/",
snmpm:load_mib(Dir ++ "MY-MIB").
```
""".
-spec load_mib(MibName) -> ok | {error, Reason} when
MibName :: snmp:mib_name(),
Reason :: term().
load_mib(MibFile) ->
snmpm_server:load_mib(MibFile).
%% Unload a mib from the manager
-doc """
Unload a `Mib` from the manager. The `MibName` is the name of the Mib, including
the path to where the compiled mib is found. For example,
```erlang
Dir = code:priv_dir(my_app) ++ "/mibs/",
snmpm:unload_mib(Dir ++ "MY-MIB").
```
""".
-spec unload_mib(MibName) -> ok | {error, Reason} when
MibName :: snmp:mib_name(),
Reason :: term().
unload_mib(MibName) ->
snmpm_server:unload_mib(MibName).
%% Which mib's are loaded
-doc """
Get a list of all the mib's loaded into the manager.
""".
-spec which_mibs() -> Mibs when
Mibs :: [{MibName, MibFile}],
MibName :: snmp:mib_name(),
MibFile :: string().
which_mibs() ->
snmpm_config:which_mibs().
%% Get all the possible oid's for the aliasname
-doc """
Transform a alias-name to its oid.
Note that an alias-name is only unique within the mib, so when loading several
mib's into a manager, there might be several instances of the same aliasname.
""".
-spec name_to_oid(AliasName) -> {ok, OIDs} | {error, Reason} when
AliasName :: atom(),
OIDs :: [snmp:oid()],
Reason :: term().
name_to_oid(AliasName) ->
snmpm_config:name_to_oid(AliasName).
%% Get the aliasname for an oid
-doc """
Transform a oid to its aliasname.
""".
-spec oid_to_name(OID) -> {ok, AliasName} | {error, Reason} when
OID :: snmp:oid(),
AliasName :: atom(),
Reason :: term().
oid_to_name(Oid) ->
snmpm_config:oid_to_name(Oid).
%% Get the type for an oid
-doc """
Retrieve the type (asn1 bertype) of an oid.
""".
-spec oid_to_type(OID) -> {ok, Type} | {error, Reason} when
OID :: snmp:oid(),
Type :: atom(),
Reason :: term().
oid_to_type(Oid) ->
snmpm_config:oid_to_type(Oid).
%% -- Info --
-doc """
Returns a list (a dictionary) containing information about the manager.
Information includes statistics counters, miscellaneous info about each process
(e.g. memory allocation), and so on.
""".
-spec info() -> [{Key, Value}] when
Key :: atom(),
Value :: term().
info() ->
snmpm_server:info().
-doc false.
info(Key) ->
proplists:get_value(Key, info(), {error, not_found}).
%% -- Verbosity --
%% Change the verbosity of a process in the manager
-doc """
Sets verbosity for the designated process. For the lowest verbosity `silence`,
nothing is printed. The higher the verbosity, the more is printed.
""".
-spec verbosity(Target, Verbosity) -> snmp:void() when
Target :: config | server | net_if | note_store | all,
Verbosity :: snmp:verbosity().
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).
%% -- Restart --
%% Restart various component processes in the manager
%% Note that the effects of this is diffiult to
%% predict, so it should be use with *caution*!
-doc """
Restart the indicated process (`What`). Note that its not without risk to
restart a process, and should therefore be used with care.
""".
-doc(#{since => <<"OTP 22.3">>}).
-spec restart(What) -> snmp:void() when
What :: net_if.
restart(net_if = What) ->
snmpm_server:restart(What).
%% -- 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, incoming reply or incoming trap/notification).
%% Note that this could have already been done as a
%% consequence of the node config.
-doc(#{equiv => register_user/4}).
-spec register_user(UserId, Module, Data) -> ok | {error, Reason} when
UserId :: user_id(),
Module :: snmpm_user(),
Data :: term(),
Reason :: term().
register_user(UserId, Module, Data) ->
register_user(UserId, Module, Data, []).
%% Default config for agents registered by this user
-doc """
Register the manager entity (=user) responsible for specific agent(s).
`Module` is the callback module (`m:snmpm_user` behaviour) which will be called
whenever something happens (detected agent, incoming reply or incoming
trap/notification).
`Data` is an opaque data structure, not inspected by the manager, that will be
included in all callback calls to the `Module` callback module (`m:snmpm_user`
behaviour).
The argument `DefaultAgentConfig` is used as default values when this user
register agents.
Note that this operation (register user) could have already been done as a
consequence of the node config. (see users.conf).
""".
-spec register_user(UserId, Module, Data, DefaultAgentConfig) ->
ok | {error, Reason} when
UserId :: user_id(),
Module :: snmpm_user(),
Data :: term(),
DefaultAgentConfig :: [DefaultConfigEntry],
DefaultConfigEntry :: {Item, Value},
Item :: agent_config_item(),
Value :: term(),
Reason :: term().
register_user(UserId, Module, Data, DefaultAgentConfig) ->
snmpm_server:register_user(UserId, Module, Data, DefaultAgentConfig).
-doc(#{equiv => register_user_monitor/4}).
-spec register_user_monitor(UserId, Module, Data) -> ok | {error, Reason} when
UserId :: user_id(),
Module :: snmpm_user(),
Data :: term(),
Reason :: term().
register_user_monitor(UserId, Module, Data) ->
register_user_monitor(UserId, Module, Data, []).
-doc """
Register the monitored manager entity (=user) responsible for specific agent(s).
The process performing the registration will be monitored. Which means that if
that process should die, all agents registered by that user process will be
unregistered. All outstanding requests will be canceled.
`Module` is the callback module (`m:snmpm_user` behaviour) which will be called
whenever something happens (detected agent, incoming reply or incoming
trap/notification).
`Data` is an opaque data structure, not inspected by the manager, that will be
included in all callback calls to the `Module` callback module (`m:snmpm_user`
behaviour).
The argument `DefaultAgentConfig` is used as default values when this user
register agents.
""".
-spec register_user_monitor(UserId, Module, Data, DefaultAgentConfig) ->
ok | {error, Reason} when
UserId :: user_id(),
Module :: snmpm_user(),
Data :: term(),
DefaultAgentConfig :: [DefaultConfigEntry],
DefaultConfigEntry :: {Item, Value},
Item :: agent_config_item(),
Value :: term(),
Reason :: term().
register_user_monitor(UserId, Module, Data, DefaultAgentConfig) ->
snmpm_server:register_user_monitor(UserId, Module,
Data, DefaultAgentConfig).
-doc """
Unregister the user.
""".
-spec unregister_user(UserId) -> ok | {error, Reason} when
UserId :: user_id(),
Reason :: term().
unregister_user(UserId) ->
snmpm_server:unregister_user(UserId).
-doc """
Get a list of the identities of all registered users.
""".
-spec which_users() -> Users when
Users :: [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).
-doc """
Explicitly instruct the manager to handle this agent, with `UserId` as the
responsible user.
Called to instruct the manager that this agent shall be handled. This function
is used when the user knows 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 (see [agents.conf](snmp_manager_config_files.md#agents)).
`TargetName` is a non-empty string, uniquely identifying the agent.
The type of `Val` depends on `Item`:
```text
[mandatory] engine_id = engine_id()
[mandatory] address = inet:ip_address() % Depends on tdomain
[optional] port = inet:port_number()
[optional] tdomain = snmp:tdomain()
[optional] community = snmp:community()
[optional] timeout = register_timeout()
[optional] max_message_size = snmp:mms()
[optional] version = snmp:version()
[optional] sec_model = snmp:sec_model()
[optional] sec_name = snmp:sec_name()
[optional] sec_level = snmp:sec_level()
```
Note that if no `tdomain` is given, the default value, `transportDomainUdpIpv4`,
is used.
Note that if no `port` is given and if `taddress` does not contain a port
number, the default value is used.
""".
-spec register_agent(UserId, TargetName, Config) -> ok | {error, Reason} when
UserId :: user_id(),
TargetName :: target_name(),
Config :: [ConfigEntry],
ConfigEntry :: {Item, Value},
Item :: agent_config_item(),
Value :: term(),
Reason :: term().
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 compatibility
%% 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 compatibility
register_agent(UserId, Addr, Config) when is_list(Config) ->
register_agent(UserId, Addr, ?DEFAULT_AGENT_PORT, Config).
%% Backward compatibility
%% Note that the agent engine id is a mandatory config option,
%% so this function *will* fail!
-doc false.
register_agent(UserId, Addr) ->
register_agent(UserId, Addr, ?DEFAULT_AGENT_PORT, []).
%% Backward compatibility
-doc false.
register_agent(UserId, Domain, Addr, Config0) when is_atom(Domain) ->
case lists:keymember(target_name, 1, Config0) of
false ->
TargetName = mk_target_name(Domain, Addr, Config0),
Config =
[{reg_type, addr_port},
{tdomain, Domain}, {taddress, Addr} | 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},
{tdomain, Domain}, {taddress, Addr} | Config1],
register_agent(UserId, TargetName, ensure_engine_id(Config2))
end;
register_agent(UserId, Ip, Port, Config) when is_integer(Port) ->
Domain = snmpm_config:default_transport_domain(),
Addr =
case snmp_conf:check_address(Domain, {Ip, Port}) of
ok ->
{Ip, Port};
{ok, FixedAddr} ->
FixedAddr
end,
register_agent(UserId, Domain, Addr, Config).
-doc """
Unregister the agent.
""".
-spec unregister_agent(UserId, TargetName) -> ok | {error, Reason} when
UserId :: user_id(),
TargetName :: target_name(),
Reason :: term().
unregister_agent(UserId, TargetName) when is_list(TargetName) ->
snmpm_config:unregister_agent(UserId, TargetName);
%% Backward compatibility functions
unregister_agent(UserId, Addr) ->
unregister_agent(UserId, Addr, ?DEFAULT_AGENT_PORT).
-doc false.
unregister_agent(UserId, DomainIp, AddressPort) ->
case target_name(DomainIp, AddressPort) of
{ok, TargetName} ->
unregister_agent(UserId, TargetName);
Error ->
Error
end.
-doc """
Retrieve agent config.
""".
-spec agent_info(TargetName, Item) -> {ok, Value} | {error, Reason} when
TargetName :: target_name(),
Item :: agent_config_item(),
Value :: term(),
Reason :: term().
agent_info(TargetName, Item) ->
snmpm_config:agent_info(TargetName, Item).
-doc """
Update agent config.
This function, [`update_agent_info/3`](`update_agent_info/3`), should be used when several
values needs to be updated atomically.
See function `register_agent/3` for more info about what kind of items are allowed.
""".
-doc(#{since => <<"OTP R14B04">>}).
-spec update_agent_info(UserId, TargetName, Info) -> ok | {error, Reason} when
UserId :: user_id(),
TargetName :: target_name(),
Info :: [{Item, Value}],
Item :: agent_config_item(),
Value :: term(),
Reason :: term().
update_agent_info(UserId, TargetName, Info) when is_list(Info) ->
snmpm_config:update_agent_info(UserId, TargetName, Info).
-doc """
Update agent config.
See function `register_agent/3` for more info about what
kind of items are allowed.
""".
-spec update_agent_info(UserId, TargetName, Item, Value) ->
ok | {error, Reason} when
UserId :: user_id(),
TargetName :: target_name(),
Item :: agent_config_item(),
Value :: term(),
Reason :: term().
update_agent_info(UserId, TargetName, Item, Val) ->
update_agent_info(UserId, TargetName, [{Item, Val}]).
-doc(#{equiv => which_agents/1}).
-spec which_agents() -> Agents when
Agents :: [target_name()].
which_agents() ->
snmpm_config:which_agents().
-doc """
Get a list of all registered agents or all agents registered by a specific user.
""".
-spec which_agents(UserId) -> Agents when
UserId :: user_id(),
Agents :: [target_name()].
which_agents(UserId) ->
snmpm_config:which_agents(UserId).
%% -- USM users --
-doc """
Explicitly instruct the manager to handle this USM user. Note that there is an
alternate way to do the same thing: Add the usm user to the manager config files
(see [usm.conf](snmp_manager_config_files.md#security-data-for-usm)).
""".
-spec register_usm_user(EngineID, UserName, Config) -> ok | {error, Reason} when
EngineID :: snmp:engine_id(),
UserName :: snmp:usm_name(),
Config :: [ConfigEntry],
ConfigEntry :: {Item, Value},
Item :: usm_config_item(),
Value :: term(),
Reason :: term().
register_usm_user(EngineID, UserName, Config)
when is_list(EngineID) andalso is_list(UserName) andalso is_list(Config) ->
snmpm_config:register_usm_user(EngineID, UserName, Config).
-doc """
Unregister this USM user.
""".
-spec unregister_usm_user(EngineID, UserName) -> ok | {error, Reason} when
EngineID :: snmp:engine_id(),
UserName :: snmp:usm_name(),
Reason :: term().
unregister_usm_user(EngineID, UserName)
when is_list(EngineID) andalso is_list(UserName) ->
snmpm_config:unregister_usm_user(EngineID, UserName).
-doc """
Retrieve usm user config.
""".
-spec usm_user_info(EngineID, UserName, Item) ->
{ok, Value} | {error, Reason} when
EngineID :: snmp:engine_id(),
UserName :: snmp:usm_name(),
Item :: usm_config_item(),
Value :: term(),
Reason :: term().
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).
-doc """
Update usm user config.