File: mib2sql

package info (click to toggle)
scotty 2.1.9-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 9,984 kB
  • ctags: 4,313
  • sloc: ansic: 35,946; sh: 12,591; tcl: 8,122; yacc: 2,442; makefile: 898; lex: 370
file content (145 lines) | stat: -rwxr-xr-x 3,194 bytes parent folder | download
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
#!/usr/local/bin/scotty -nf
## -*- tcl -*-
##
## Convert Internet MIB definitions into a SQL database schema.
## This script output create table SQL statements. Every relation
## contains a column IpAddresse and a column TimeStamp which is used 
## to mark the time the data row was retrieved.
##
## Copyright (c) 1995
##
## J. Schoenwaelder
## TU Braunschweig, Germany
## Institute for Operating Systems and Computer Networks
##
## Permission to use, copy, modify, and distribute this
## software and its documentation for any purpose and without
## fee is hereby granted, provided that this copyright
## notice appears in all copies.  The University of Braunschweig
## makes no representations about the suitability of this
## software for any purpose.  It is provided "as is" without
## express or implied warranty.
##

##
## The following proc converts an Internet MIB type (aka syntax)
## into a SQL type. Feel free to edit the switch to fit your
## database system.
##

proc SQLtype {oid} {
    set syntax [mib syntax $oid]
    if {[mib tc $oid] != ""} {
	set syntax [lindex [mib tc $oid] 1]
    }
    switch -glob $syntax {
	INTEGER -
	UInteger* -
	Integer* -
	Gauge* -
	Counter* -
	TimeTicks {
	    return int
	}
	IpAddress {
	    return char(4)
	}
	"OCTET STRING" {
	    return char(256)
	}
	"OBJECT IDENTIFIER" {
	    return char(256)
	}
	PhysAddress {
	    return char(80)
	}
	NetworkAddress {
	    return char(80)
	}
	NsapAddress {
	    return char(80)
	}
	default {
	    error "failed to translate $syntax to SQL type"
	}
    }
}

##
## Construct an SQL statement to create a table that represents
## the SNMP table given by oid.
##

proc SQLtable {oid} {
    set name [mib name $oid]
    append sql "create table $name ( \n"
    append sql "\tTarget\tchar(15) not null,\n"
    append sql "\tTimeStamp\tint not null"
    foreach var [mib suc [mib suc $oid]] {
	set attribute [mib name $var]
	append sql ",\n\t$attribute\t[SQLtype $var]"
    }
    append sql "\n) \\g\n"
    return $sql
}

##
## Construct an SQL statement to create a table that represents
## the SNMP scalars in the group given by oid.
##

proc SQLscalars {oid} {
    set name [mib name $oid]
    append sql "create table $name ( \n"
    append sql "\tTarget\tint not null,\n"
    append sql "\tTimeStamp\tint not null"
    foreach var [mib suc $oid] {
	if {[mib access $var] != "not-accessible"} {
	    set attribute [mib name $var]
	    append sql ",\n\t$attribute\t[SQLtype $var]"
	}
    }
    append sql "\n) \\g\n"
    return $sql
}

##
## Walk through a MIB tree and create tables to hold SNMP tables
## and SNMP scalars.
##

proc walk {node} {
    mib walk oid [mib oid $node] {
	switch [mib syntax $oid] {
	    SEQUENCE continue
	    "SEQUENCE OF" {
		puts [SQLtable $oid]
	    }
	    "OBJECT IDENTIFIER" {
		set scalars 0
		foreach var [mib suc $oid] {
		    if {[mib access $var] != "not-accessible"} {
			set scalars 1
			break
		    }
		}
		if {$scalars} {
		    puts [SQLscalars $oid]
		}
	    }
	}
    }
}

##
## And that's all. Check the arguments and go ahead.
##

if {$argv == ""} {
    puts stderr {usage: mib2sql <mib-node> [<mib-node> ...]}
    exit 42
}

foreach node $argv {
    walk $node
}