Created
January 14, 2016 20:38
-
-
Save fuzzy/1b83d874d66d6042f5ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2014, Mike 'Fuzzy' Partin <[email protected]> | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"regexp" | |
"strings" | |
) | |
type Target struct { | |
CarbonPrefix string `json:"carbon_prefix"` | |
Hostname string `json:"hostname"` | |
Tables []struct { | |
Name string `json:"name"` | |
BaseOid string `json:"base_oid"` | |
LabelOid int `json:"label_oid"` | |
OidEntries []struct { | |
Oid int `json:"oid"` | |
Path string `json:"path"` | |
} `json:"entries"` | |
} `json:"tables"` | |
Metrics []struct { | |
Oid string `json:"oid"` | |
Path string `json:"path"` | |
} | |
} | |
type Config struct { | |
Daemonize bool `json:"daemonize"` | |
LogDir string `json:"log_dir"` | |
CfgDir string `json:"cfg_dir"` | |
LogName string `json:"log_name"` | |
Community string `json:"snmp_community"` | |
CarbonHost string `json:"carbon_host"` | |
CarbonPort int `json:"carbon_port"` | |
CarbonPrefix string `json:"carbon_prefix"` | |
Targets []Target | |
} | |
func readFile(fpath string) []byte { | |
// define our return variable | |
var data []byte | |
// and our comment syntax | |
comments := regexp.MustCompile("//.*$") | |
// and our file descriptor | |
fp, err := os.Open(fpath) | |
if err != nil { | |
panic(err) | |
} | |
// and finally our line by line scanner | |
scanner := bufio.NewScanner(fp) | |
// now let's get to processing | |
for scanner.Scan() { | |
// strip all whitespace, and comments | |
line := comments.ReplaceAllString(strings.TrimSpace(scanner.Text()), "") | |
// store all valid data in our return variable | |
for _, c := range line { | |
data = append(data[:], byte(c)) | |
} | |
} | |
// one last status check | |
err = scanner.Err() | |
if err != nil { | |
panic(err) | |
} | |
// and return | |
return data | |
} | |
func ReadConfig(jsonCfg string) Config { | |
// declare our return variable | |
var cfg Config | |
// read in our config and unmarshal it, or die tryin | |
err := json.Unmarshal(readFile(jsonCfg), &cfg) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("Polling SNMP Community: %s\n", cfg.Community) | |
fmt.Printf("Using graphite instance at: %s:%d\n", cfg.CarbonHost, cfg.CarbonPort) | |
dir, er := ioutil.ReadDir(cfg.CfgDir) | |
if er != nil { | |
panic(er) | |
} else { | |
for key := range dir { | |
if strings.HasSuffix(dir[key].Name(), ".json") { | |
fmt.Printf("Reading: %s/%s\n", cfg.CfgDir, dir[key].Name()) | |
var tCfg Target | |
err = json.Unmarshal(readFile(fmt.Sprintf("%s/%s", cfg.CfgDir, dir[key].Name())), &tCfg) | |
if err != nil { | |
panic(err) | |
} | |
cfg.Targets = append(cfg.Targets, tCfg) | |
} | |
} | |
} | |
return cfg | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// | |
"hostname": "corp-access01.corp.theplatform.com", | |
"carbon_prefix": "corp-access01", | |
"tables": [ | |
{ | |
"name": "ports", | |
"base_oid": "1.3.6.1.2.1.2.2.1", | |
"label_oid": 2, | |
"entries": [ | |
{"oid": 10, "path": "octets.in"}, | |
{"oid": 16, "path": "octets.out"}, | |
{"oid": 14, "path": "errors.in"}, | |
{"oid": 20, "path": "errors.out"} | |
] | |
} | |
], | |
"metrics": [ | |
{"oid": "1.2.3.4.5.6.7.8.9.10", "path": "specific.metric.path"} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// As a quick note on syntax, this is a comment... | |
// This has no effect at current, but will....oh yes, and soon | |
"daemonize": true, | |
// This is kind of in the same boat | |
"log_dir": "/Users/fuzzy/s2g/log", | |
// This however should point to a directory where you will keep config files | |
// on a per host basis. Any file in this directory with the extension '.json' | |
// will be read in. | |
"cfg_dir": "/Users/fuzzy/s2g/etc/s2g.d", | |
// This is currently set as a global option, but I will be moving it to the | |
// host configs at some point, with the global option only set as a default. | |
"snmp_community": "tPnet0ps", | |
// Where should I shove my metrics | |
"carbon_host": "192.168.106.34", | |
"carbon_port": 2003, | |
// All our metric paths should begin with: | |
"carbon_prefix": "network" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment