gosmi

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2019 License: MIT Imports: 9 Imported by: 0

README

gosmi

Go wrapper around libsmi

Usage

On Ubuntu: $ sudo apt-get install libsmi2-dev

Example
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/sleepinggenius2/gosmi"
)

type arrayStrings []string

var modules arrayStrings
var paths arrayStrings
var debug bool

func (a arrayStrings) String() string {
	return strings.Join(a, ",")
}

func (a *arrayStrings) Set(value string) error {
	*a = append(*a, value)
	return nil
}

func main() {
	flag.BoolVar(&debug, "d", false, "Debug")
	flag.Var(&modules, "m", "Module to load")
	flag.Var(&paths, "p", "Path to add")
	flag.Parse()

	Init()

	oid := flag.Arg(0)
	if oid == "" {
		ModuleTrees()
	} else {
		Subtree(oid)
	}

	Exit()
}

func Init() {
	gosmi.Init()

	for _, path := range paths {
		gosmi.AppendPath(path)
	}

	for _, module := range modules {
		moduleName, err := gosmi.LoadModule(module)
		if err != nil {
			fmt.Println("Error: %s\n", err)
			return
		}
		if debug {
			fmt.Printf("Loaded module %s\n", moduleName)
		}
	}

	if debug {
		path := gosmi.GetPath()
		fmt.Printf("Search path: %s\n", path)
		loadedModules := gosmi.GetLoadedModules()
		fmt.Println("Loaded modules:")
		for _, loadedModule := range loadedModules {
			fmt.Printf("  %s (%s)\n", loadedModule.Name, loadedModule.Path)
		}
	}
}

func Exit() {
	gosmi.Exit()
}

func Subtree(oid string) {
	node, err := gosmi.GetNode(oid)
	if err != nil {
		fmt.Printf("Subtree Error: %s", err)
		return
	}

	subtree := node.GetSubtree()

	jsonBytes, _ := json.Marshal(subtree)
	os.Stdout.Write(jsonBytes)
}

func ModuleTrees() {
	for _, module := range modules {
		m, err := gosmi.GetModule(module)
		if err != nil {
			fmt.Printf("Module Trees Error: %s\n", err)
			continue
		}

		nodes := m.GetNodes()
		types := m.GetTypes()

		jsonBytes, _ := json.Marshal(struct {
			Module gosmi.Module
			Nodes  []gosmi.Node
			Types  []gosmi.Type
		}{
			Module: m,
			Nodes:  nodes,
			Types:  types,
		})
		os.Stdout.Write(jsonBytes)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendPath

func AppendPath(path string)

func Exit

func Exit()

func GetPath

func GetPath() string

func Init

func Init()

func IsLoaded

func IsLoaded(moduleName string) bool

func LoadModule

func LoadModule(modulePath string) (moduleName string, err error)

func PrependPath

func PrependPath(path string)

func ReadConfig

func ReadConfig(filename string, tag ...string) bool

func SetPath

func SetPath(path string)

Types

type Notification

type Notification struct {
	SmiNode
	Objects []SmiNode
}

type SmiModule

type SmiModule struct {
	models.Module
	// contains filtered or unexported fields
}

func CreateModule

func CreateModule(smiModule *C.struct_SmiModule) (module SmiModule)

func GetLoadedModules

func GetLoadedModules() (modules []SmiModule)

func GetModule

func GetModule(name string) (module SmiModule, err error)

func (SmiModule) GetIdentityNode

func (m SmiModule) GetIdentityNode() (node SmiNode, ok bool)

func (SmiModule) GetImports

func (m SmiModule) GetImports() (imports []models.Import)

func (SmiModule) GetNode

func (m SmiModule) GetNode(name string) (node SmiNode, err error)

func (SmiModule) GetNodes

func (m SmiModule) GetNodes(kind ...types.NodeKind) (nodes []SmiNode)

func (SmiModule) GetRaw

func (m SmiModule) GetRaw() (module *C.struct_SmiModule)

func (SmiModule) GetRevisions

func (m SmiModule) GetRevisions() (revisions []models.Revision)

func (SmiModule) GetType

func (m SmiModule) GetType(name string) (outType SmiType, err error)

func (SmiModule) GetTypes

func (m SmiModule) GetTypes() (types []SmiType)

func (*SmiModule) SetRaw

func (m *SmiModule) SetRaw(smiModule *C.struct_SmiModule)

type SmiNode

type SmiNode struct {
	models.Node

	SmiType *SmiType
	// contains filtered or unexported fields
}

func CreateNode

func CreateNode(smiNode *C.struct_SmiNode) (node SmiNode)

func GetNode

func GetNode(name string, module ...SmiModule) (node SmiNode, err error)

func (SmiNode) AsNotification

func (n SmiNode) AsNotification() Notification

func (SmiNode) AsTable

func (t SmiNode) AsTable() Table

func (SmiNode) GetAugment

func (t SmiNode) GetAugment() (row SmiNode)

func (SmiNode) GetColumns

func (t SmiNode) GetColumns() (columns map[string]SmiNode, columnOrder []string)

func (SmiNode) GetImplied

func (t SmiNode) GetImplied() (implied bool)

func (SmiNode) GetIndex

func (t SmiNode) GetIndex() (index []SmiNode)

func (SmiNode) GetModule

func (n SmiNode) GetModule() (module SmiModule)

func (SmiNode) GetNotificationObjects

func (n SmiNode) GetNotificationObjects() (objects []SmiNode)

func (SmiNode) GetRaw

func (n SmiNode) GetRaw() (node *C.struct_SmiNode)

func (SmiNode) GetRow

func (t SmiNode) GetRow() (row SmiNode)

func (SmiNode) GetSubtree

func (n SmiNode) GetSubtree() (nodes []SmiNode)

func (SmiNode) Render

func (n SmiNode) Render(flags types.Render) string

func (SmiNode) RenderNumeric

func (n SmiNode) RenderNumeric() string

func (SmiNode) RenderQualified

func (n SmiNode) RenderQualified() string

func (*SmiNode) SetRaw

func (n *SmiNode) SetRaw(smiNode *C.struct_SmiNode)

type SmiType

type SmiType struct {
	models.Type
	// contains filtered or unexported fields
}

func CreateType

func CreateType(smiType *C.struct_SmiType) (outType SmiType)

func CreateTypeFromNode

func CreateTypeFromNode(smiNode *C.struct_SmiNode) (outType *SmiType)

func GetType

func GetType(name string, module ...SmiModule) (outType SmiType, err error)

func (SmiType) GetModule

func (t SmiType) GetModule() (module SmiModule)

func (SmiType) GetRaw

func (t SmiType) GetRaw() (outType *C.struct_SmiType)

func (*SmiType) SetRaw

func (t *SmiType) SetRaw(smiType *C.struct_SmiType)

func (SmiType) String

func (t SmiType) String() string

type Table

type Table struct {
	SmiNode
	Columns     map[string]SmiNode
	ColumnOrder []string
	Implied     bool
	Index       []SmiNode
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL