aboutsummaryrefslogtreecommitdiffstats
path: root/lib/snmp/src/agent/snmpa_mib_lib.erl
blob: 700bdd1a6e2adc7d41fa47c21ad790ee39cfd07a (plain) (blame)
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2004-2009. 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(snmpa_mib_lib).

-export([table_cre_row/3, table_del_row/2]).
-export([get_table/2]).
-export([print_variables/1, print_table/3, print_table/4, print_tables/1]).
-export([gc_tab/3, gc_tab/5]).

-include("SNMPv2-TC.hrl").
-include("snmp_types.hrl").

-define(VMODULE,"MIB-LIB").
-include("snmp_verbosity.hrl").

-ifndef(default_verbosity).
-define(default_verbosity,silence).
-endif.


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

%% returns: bool()
table_cre_row({Tab, mnesia}, Key, _Row) ->
    ?vtrace("create mnesia table ~w row with Key: ~w",[Tab, Key]),
    {error, mnesia_not_supported};
table_cre_row({Tab, Db} = TabDb, Key, Row) ->
    ?vtrace("create ~w table ~w row with Key: ~w",[Db, Tab, Key]),
    snmpa_local_db:table_create_row(TabDb, Key, Row).

%% returns: bool()
table_del_row({Tab, mnesia}, Key) ->
    ?vtrace("delete mnesia table ~w row with Key: ~w",[Tab, Key]),
    {error, mnesia_not_supported};
table_del_row({Tab, Db} = TabDb, Key) ->
    ?vtrace("delete ~w table ~w row with Key: ~w", [Db, Tab, Key]),
    snmpa_local_db:table_delete_row(TabDb, Key).


%%%-----------------------------------------------------------------
%%% Retreives the entire table. Used for debugging
%%%-----------------------------------------------------------------

get_table(NameDb, FOI) ->
    (catch get_table(NameDb, FOI, [], [])).

get_table(NameDb, FOI, Oid, Acc) ->
    case table_next(NameDb, Oid) of
        endOfTable ->
            ?vdebug("end of table",[]),
            {ok, lists:reverse(Acc)};
        Oid ->
            %% Crap, circular ref
            ?vinfo("cyclic reference: ~w -> ~w", [Oid,Oid]),
            throw({error, {cyclic_db_reference, Oid, Acc}});
        NextOid ->
            ?vtrace("get row for oid ~w", [NextOid]),
            case table_get_row(NameDb, NextOid, FOI) of
                undefined -> 
		    throw({error, {invalid_rowindex, NextOid, Acc}});
                Row ->
                    ?vtrace("row: ~w", [Row]),
		    get_table(NameDb, FOI, NextOid, [{NextOid, Row}|Acc])
            end
    end.
    

print_variables(Variables) when is_list(Variables) ->
    Variables2 = print_variables_prefixify(Variables), 
    lists:foreach(fun({Variable, ValueResult, Prefix}) ->
			  print_variable(Variable, ValueResult, Prefix)
		  end, Variables2),
    ok.

print_variable(Variable, {value, Val}, Prefix) when is_atom(Variable) ->
    io:format("~w~s=> ~p~n", [Variable, Prefix, Val]);
print_variable(Variable, Error, Prefix) when is_atom(Variable) ->
    io:format("~w~s=> [e] ~p~n", [Variable, Prefix, Error]).

print_variables_prefixify(Variables) ->
    MaxVarLength = print_variables_maxlength(Variables),
    print_variables_prefixify(Variables, MaxVarLength, []).

print_variables_prefixify([], _MaxVarLength, Acc) ->
    lists:reverse(Acc);
print_variables_prefixify([{Var, Res}|Variables], MaxVarLength, Acc) ->
    Prefix = make_variable_print_prefix(Var, MaxVarLength),
    print_variables_prefixify(Variables, MaxVarLength, 
			      [{Var, Res, Prefix}|Acc]).
    
make_variable_print_prefix(Var, MaxVarLength) ->
    lists:duplicate(MaxVarLength - length(atom_to_list(Var)) + 1, $ ).

print_variables_maxlength(Variables) ->
    print_variables_maxlength(Variables, 0).

print_variables_maxlength([], MaxLength) ->
    MaxLength;
print_variables_maxlength([{Var, _}|Variables], MaxLength) when is_atom(Var) ->
    VarLen = length(atom_to_list(Var)),
    if 
	VarLen > MaxLength ->
	    print_variables_maxlength(Variables, VarLen);
	true ->
	    print_variables_maxlength(Variables, MaxLength)
    end.


print_tables(Tables) when is_list(Tables) ->
    lists:foreach(fun({Table, DB, FOI, PrintRow}) ->
			  print_table(Table, DB, FOI, PrintRow)
		  end, Tables),
    ok.

%% print_table(Table, DB, FOI, PrintRow) ->
%%     TableInfo = get_table(DB(Table), FOI(Table)),
%%     print_table(Table, TableInfo, PrintRow),
%%     ok.

print_table(Table, DB, FOI, PrintRow) ->
    TableInfo = get_table(DB, FOI),
    print_table(Table, TableInfo, PrintRow).

print_table(Table, TableInfo, PrintRow) when is_function(PrintRow, 2) ->
    io:format("~w =>", [Table]),
    do_print_table(TableInfo, PrintRow).

do_print_table({ok, [] = _TableInfo}, _PrintRow) ->
    io:format(" -~n", []);
do_print_table({ok, TableInfo}, PrintRow) when is_function(PrintRow, 2) ->
    io:format("~n", []),
    lists:foreach(fun({RowIdx, Row}) ->
			  io:format("   ~w => ~n~s~n", 
				    [RowIdx, PrintRow("      ", Row)])
		  end, TableInfo),
    io:format("~n", []);
do_print_table({error, {invalid_rowindex, BadRowIndex, []}}, _PrintRow) ->
    io:format("Error: Bad rowindex ~w~n", [BadRowIndex]);
do_print_table({error, {invalid_rowindex, BadRowIndex, TableInfo}}, PrintRow) ->
    io:format("Error: Bad rowindex ~w", [BadRowIndex]),
    do_print_table(TableInfo, PrintRow);
do_print_table(Error, _PrintRow) ->
    io:format("Error: ~p~n", [Error]).
    

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

table_next({Name, mnesia}, RestOid) ->
    snmp_generic_mnesia:table_next(Name, RestOid);
table_next(NameDb, RestOid) -> 
    snmpa_local_db:table_next(NameDb, RestOid).


table_get_row({Name, mnesia}, RowIndex) ->
    snmp_generic_mnesia:table_get_row(Name, RowIndex);
table_get_row(NameDb, RowIndex) ->
    snmpa_local_db:table_get_row(NameDb, RowIndex).

table_get_row(NameDb, RowIndex, undefined) ->
    table_get_row(NameDb, RowIndex);
table_get_row({Name, mnesia}, RowIndex, FOI) ->
    snmp_generic_mnesia:table_get_row(Name, RowIndex, FOI);
table_get_row(NameDb, RowIndex, _FOI) ->
    snmpa_local_db:table_get_row(NameDb, RowIndex).


%%%-----------------------------------------------------------------
%%% Utility module for the mib-implementation modules (such as
%%% snmp_target_mib).
%%%-----------------------------------------------------------------

gc_tab(TabDb, STC, FOI) ->
    InvalidateRow = fun(_) -> ok end,
    UpdateRow     = fun(_) -> ok end,
    gc_tab(TabDb, STC, FOI, InvalidateRow, UpdateRow).

gc_tab({Tab,mnesia} = TabDb, STC, FOI, InvalidateRow, UpdateRow) ->
    F = fun(RowIndex, Row) ->
		case element(STC, Row) of
                    ?'StorageType_volatile' ->
			snmp_generic_mnesia:table_delete_row(Tab, RowIndex),
                        InvalidateRow(RowIndex);
		    _ ->
			UpdateRow(RowIndex)
		end
	end,
    gc_tab1(F, TabDb, FOI);

gc_tab(TabDb, STC, FOI, InvalidateRow, UpdateRow) ->
    F = fun(RowIndex, Row) ->
		case element(STC, Row) of
 		    ?'StorageType_volatile' ->
			snmpa_local_db:table_delete_row(TabDb, RowIndex),
			InvalidateRow(RowIndex);
		    _ ->
			UpdateRow(RowIndex),
			ok
		end
	end,
    gc_tab1(F, TabDb, FOI).


gc_tab1(F, {Tab,_} = TabDb, FOI) ->
    case (catch snmp_generic:table_foreach(TabDb, F, FOI)) of
	{'EXIT',{cyclic_db_reference,Oid}} ->
	    %% Remove the row regardless of storage type since this
	    %% is a major error. This row must be removed.
	    case table_delete_row(TabDb, Oid) of
		true -> 
		    ?vlog("deleted cyclic ref row for: ~w;~w",
			  [Tab, Oid]),
		    config_err("cyclic reference in table ~w: "
			       "~w -> ~w. Row deleted", 
			       [Tab, Oid, Oid]),
		    gc_tab1(F, TabDb, FOI);
		false ->
		    ?vlog("unable to remove faulty row from table ~w",
			  [Tab]),
		    config_err("failed removing faulty row. "
			       "Giving up on table ~w cleanup", [Tab])
	    end;
	_ ->
	    ok
    end.

table_delete_row({Tab, mnesia}, Oid) ->
    snmp_generic_mnesia:table_delete_row(Tab, Oid),
    true;
table_delete_row(TabDb, Oid) ->
    snmpa_local_db:table_delete_row(TabDb, Oid).

config_err(F, A) ->
    snmpa_error:config_err(F, A).