Skip to content

Commit a5a345d

Browse files
nblair2GhostofGoes
authored andcommitted
feat(vrouter): SNMP configuration vyos/vyatta
Adds ability to turn on and configure snmp via the vrouter app for vyos or vyatta hosts.
1 parent 5792e16 commit a5a345d

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

src/go/app/vrouter.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ type DHCPConfig struct {
5151
Static map[string]string `mapstructure:"staticAssignments"`
5252
}
5353

54+
type SNMPConfig struct {
55+
ListenAddr string `mapstructure:"listenAddress"`
56+
SystemName string `mapstructure:"systemName"`
57+
Location string `mapstructure:"location"`
58+
Contact string `mapstructure:"contact"`
59+
Communities []struct {
60+
Name string `mapstructure:"name"`
61+
Authorization string `mapstructure:"authorization"`
62+
Clients []string `mapstructure:"clients"`
63+
TrapTargets []string `mapstructure:"trapTargets"`
64+
} `mapstructure:"communities"`
65+
}
66+
5467
type Emulator struct {
5568
Ingress []string `mapstructure:"ingress"`
5669
Egress []string `mapstructure:"egress"`
@@ -247,6 +260,13 @@ func (this *Vrouter) PreStart(ctx context.Context, exp *types.Experiment) error
247260
data["emulators"] = emulators
248261
}
249262

263+
snmp, err := this.processSNMP(md)
264+
if err != nil {
265+
return fmt.Errorf("processing SNMP metadata for host %s: %w", host.Hostname(), err)
266+
}
267+
268+
data["snmp"] = snmp
269+
250270
sources, destinations, err := this.processNAT(md, node.Network().Interfaces())
251271
if err != nil {
252272
return fmt.Errorf("processing NAT metadata for host %s: %w", host.Hostname(), err)
@@ -258,6 +278,8 @@ func (this *Vrouter) PreStart(ctx context.Context, exp *types.Experiment) error
258278
break
259279
}
260280
}
281+
282+
break
261283
}
262284
}
263285

@@ -702,6 +724,32 @@ func (this *Vrouter) processIPSec(md map[string]interface{}, nets []ifaces.NodeN
702724
return &ipsec, nil
703725
}
704726

727+
func (Vrouter) processSNMP(md map[string]interface{}) (*SNMPConfig, error) {
728+
raw, ok := md["snmp"]
729+
if !ok {
730+
return nil, nil
731+
}
732+
733+
var snmp SNMPConfig
734+
if err := mapstructure.Decode(raw, &snmp); err != nil {
735+
return nil, fmt.Errorf("decoding SNMP config: %w", err)
736+
}
737+
738+
for cidx := range snmp.Communities {
739+
community := &snmp.Communities[cidx]
740+
if community.Name == "" {
741+
return nil, fmt.Errorf("snmp community at index %d must include a name", cidx)
742+
}
743+
744+
}
745+
746+
if snmp.ListenAddr == "" && snmp.SystemName == "" && snmp.Location == "" && snmp.Contact == "" && len(snmp.Communities) == 0 {
747+
return nil, nil
748+
}
749+
750+
return &snmp, nil
751+
}
752+
705753
func (this *Vrouter) processNAT(md map[string]interface{}, nets []ifaces.NodeNetworkInterface) ([]NATRule, []NATRule, error) {
706754
var (
707755
sources []NATRule

src/go/tmpl/templates/vyatta.tmpl

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{{- $vyos := index . "vyos" -}}
55
{{- $passwd := index . "passwd" -}}
66
{{- $ssh := index . "ssh" -}}
7+
{{- $snmp := index . "snmp" -}}
78
{{- $emulators := index . "emulators" -}}
89
{{- $snat := index . "snat" -}}
910
{{- $dnat := index . "dnat" -}}
@@ -221,14 +222,47 @@ set vpn ipsec site-to-site peer {{ $site.Peer }} tunnel {{ $idx }} remote prefix
221222
{{- end }}
222223
{{- end }}
223224
# -------------------------------- Services -------------------------------
225+
# SSH
224226
{{- if $ssh }}
225227
set service ssh listen-address {{ $ssh }}
226228
{{- end }}
229+
# NTP
227230
{{- if $ntpAddr }}
228231
set service ntp server {{ $ntpAddr }} prefer
229232
{{- else }}
230233
delete service ntp
231234
{{- end }}
235+
# SNMP
236+
{{- if $snmp }}
237+
{{- if $snmp.ListenAddr }}
238+
set service snmp listen-address {{ $snmp.ListenAddr }}
239+
{{- end }}
240+
{{- if $snmp.Contact }}
241+
set service snmp contact '{{ $snmp.Contact }}'
242+
{{- end }}
243+
{{- if $snmp.Location }}
244+
set service snmp location '{{ $snmp.Location }}'
245+
{{- end }}
246+
{{- if $snmp.SystemName }}
247+
set service snmp description '{{ $snmp.SystemName }}'
248+
{{- end }}
249+
{{- range $community := $snmp.Communities }}
250+
{{- if $community.Name }}
251+
set service snmp community '{{ $community.Name }}'
252+
{{- if $community.Authorization }}
253+
set service snmp community '{{ $community.Name }}' authorization {{ $community.Authorization }}
254+
{{- else }}
255+
set service snmp community '{{ $community.Name }}' authorization ro
256+
{{- end }}
257+
{{- range $client := $community.Clients }}
258+
set service snmp community '{{ $community.Name }}' client {{ $client }}
259+
{{- end }}
260+
{{- range $target := $community.TrapTargets }}
261+
set service snmp trap-target {{ $target }} community '{{ $community.Name }}'
262+
{{- end }}
263+
{{- end }}
264+
{{- end }}
265+
{{- end }}
232266
# --------------------------------- System --------------------------------
233267
set system host-name {{ $node.RouterName }}
234268
commit
@@ -552,11 +586,48 @@ vpn {
552586
{{- end }}
553587
}
554588

555-
{{- if $ssh }}
589+
{{- if or $ssh $snmp }}
556590
service {
591+
{{- if $ssh }}
557592
ssh {
558593
listen-address {{ $ssh }}
559594
}
595+
{{- end}}
596+
{{- if $snmp }}
597+
snmp {
598+
{{- if $snmp.ListenAddr }}
599+
listen-address {{ $snmp.ListenAddr }}
600+
{{- end }}
601+
{{- if $snmp.Contact }}
602+
contact "{{ $snmp.Contact }}"
603+
{{- end }}
604+
{{- if $snmp.Location }}
605+
location "{{ $snmp.Location }}"
606+
{{- end }}
607+
{{- if $snmp.SystemName }}
608+
description "{{ $snmp.SystemName }}"
609+
{{- end }}
610+
{{- range $community := $snmp.Communities }}
611+
{{- if $community.Name }}
612+
community {{ $community.Name }} {
613+
{{- if $community.Authorization }}
614+
authorization {{ $community.Authorization }}
615+
{{- else }}
616+
authorization ro
617+
{{- end }}
618+
{{- range $client := $community.Clients }}
619+
client {{ $client }}
620+
{{- end }}
621+
}
622+
{{- range $target := $community.TrapTargets }}
623+
trap-target {{ $target }} {
624+
community "{{ $community.Name }}"
625+
}
626+
{{- end }}
627+
{{- end }}
628+
{{- end }}
629+
}
630+
{{- end}}
560631
}
561632
{{- end }}
562633

0 commit comments

Comments
 (0)