-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (162 loc) · 4.05 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"os"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/Sirupsen/logrus"
"github.com/alouca/gosnmp"
"golang.org/x/net/context"
)
var snmp SnmpManager
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s MOUNTPOINT\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
var err error
// getopt
flag.Usage = usage
flag.Parse()
if flag.NArg() != 2 {
usage()
os.Exit(2)
}
mountpoint := flag.Arg(0)
snmpServer := flag.Arg(1)
// connect snmp
snmp.client, err = gosnmp.NewGoSNMP(snmpServer, "public", gosnmp.Version2c, 5)
if err != nil {
logrus.Fatalf("gosnmp.NewGoSNMP: %v", err)
}
//snmp.client.SetDebug(true)
//snmp.client.SetVerbose(true)
snmp.currentId = 1
snmp.cache = make(map[string]SnmpCacheEntry)
snmp.cacheMap = make(map[uint64]string)
// preload initial cache
err = snmp.LoadWalk(".1.3.6.1")
if err != nil {
logrus.Fatalf("snmp.LoadWalk: %v", err)
}
// mount fuse
c, err := fuse.Mount(
mountpoint,
fuse.FSName("fuse-snmp"),
fuse.Subtype("snmpfs"),
fuse.LocalVolume(),
fuse.VolumeName("Fuse SNMP"),
)
if err != nil {
logrus.Fatalf("fuse.Mount: %v", err)
}
defer c.Close()
// map fuse
err = fs.Serve(c, FS{})
if err != nil {
logrus.Fatalf("fs.Serve: %v", err)
}
// wait for fuse close
<-c.Ready
if err := c.MountError; err != nil {
logrus.Fatalf("c.MountError: %v", err)
}
logrus.Fatalf("BYEBYE")
}
// SnmpManger communicates with a snmp server and keep cache for resolved OIDs
type SnmpManager struct {
client *gosnmp.GoSNMP
currentId uint64
cache map[string]SnmpCacheEntry
cacheMap map[uint64]string
}
type SnmpCacheEntry struct {
pdu gosnmp.SnmpPDU
inode uint64
}
func (s *SnmpManager) LoadWalk(oid string) error {
results, err := snmp.client.BulkWalk(8, oid)
if err != nil {
logrus.Fatalf("snmp.BulkWalk: %v", err)
}
for _, v := range results {
if entry := s.cache[v.Name]; entry.inode > 0 {
logrus.Infof("MATCHED: %s", v.Name)
} else {
entry := SnmpCacheEntry{
pdu: v,
inode: s.currentId,
}
s.currentId++
s.cache[v.Name] = entry
s.cacheMap[entry.inode] = v.Name
logrus.Infof("NOT MATCHED: %s", v.Name)
}
}
return nil
}
// FS implements the snmpfs file system.
type FS struct{}
func (FS) Root() (fs.Node, error) {
logrus.Infof("Root")
return Dir{}, nil
}
// Dir implements a snmpfs directory
type Dir struct{}
func (Dir) Attr(ctx context.Context, a *fuse.Attr) error {
logrus.Infof("Dir.Attr: ctx=%q attr:%q", ctx, a)
a.Inode = 1
a.Mode = os.ModeDir | 0555
return nil
}
func (Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
logrus.Infof("Dir.Lookup: ctx=%q, name=%q", ctx, name)
if entry := snmp.cache[name]; entry.inode > 0 {
return &File{Inode: entry.inode}, nil
}
return nil, fuse.ENOENT
}
func (Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
logrus.Infof("Dir.ReadDirAll: ctx=%q", ctx)
dir := []fuse.Dirent{}
for _, entry := range snmp.cache {
dir = append(dir, fuse.Dirent{
Inode: entry.inode,
Name: entry.pdu.Name,
Type: fuse.DT_File,
})
}
return dir, nil
}
// File implements a snmpfs file
type File struct {
Inode uint64
}
func (f *File) Attr(ctx context.Context, a *fuse.Attr) error {
a.Inode = f.Inode
// FIXME: compute the size dynamically
a.Size = uint64(100)
a.Mode = 0444
logrus.Infof("File.Attr: ctx=%q, a=%q", ctx, a)
return nil
}
func (f *File) ReadAll(ctx context.Context) ([]byte, error) {
logrus.Infof("File.ReadAll: ctx=%q, file=%q", ctx, f)
oid := snmp.cacheMap[f.Inode]
entry := snmp.cache[oid].pdu
switch entry.Type {
case gosnmp.OctetString:
return []byte(entry.Value.(string) + "\n"), nil
case gosnmp.TimeTicks:
return []byte(fmt.Sprintf("%d", entry.Value) + "\n"), nil
case gosnmp.ObjectIdentifier:
// FIXME: print full oid
// BONUS: create a symbolic link !
return []byte(fmt.Sprintf("%d", entry.Value) + "\n"), nil
default:
logrus.Warnf("Response: %q %q %q", entry.Name, entry.Value, entry.Type.String())
}
return []byte(fmt.Sprintf("error unknown type: %s", entry.Type.String())), nil
}