Created
October 1, 2015 00:42
-
-
Save kmjones1979/33ce6be12a567a8a61e2 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
package main | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
"github.com/cactus/go-statsd-client/statsd" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"time" | |
) | |
type NginxResponse struct { | |
Connections struct { | |
Accepted int64 `json:"accepted"` | |
Active int64 `json:"active"` | |
Dropped int64 `json:"dropped"` | |
Idle int64 `json:"idle"` | |
} `json:"connections"` | |
} | |
func NginxStatus() (*NginxResponse, error) { | |
// assign variable for nginx plus server | |
var nginxStatusServer string = "my.nginx.server.com" | |
// perform a request to the NGINX Plus status API | |
resp, err := http.Get(fmt.Sprintf("http://%s/status", nginxStatusServer)) | |
if err != nil { | |
return nil, err | |
} | |
if resp.StatusCode != http.StatusOK { | |
return nil, errors.New("Non 200 OK") | |
} | |
// clean up the connection | |
defer resp.Body.Close() | |
// read the body of the request into a variable | |
data, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return nil, err | |
} | |
// unmarshall the JSON data into a variable | |
var er NginxResponse | |
if err := json.Unmarshal(data, &er); err != nil { | |
return nil, err | |
} | |
return &er, nil | |
} | |
func SendStatsD(gs string, gt string, gv int64) { | |
// assign variables for statsd server and metric name prefix | |
var statsdServer string = "127.0.0.1:8125" | |
var gk string = "nginx" | |
// connect to statsd | |
client, err := statsd.NewClient(statsdServer, gk) | |
if err != nil { | |
log.Println(err) | |
} | |
// clean up the connections | |
defer client.Close() | |
// assign variables for metric name and interval | |
var fi float32 = 1.0 | |
var st string = "status" | |
var sn string = "my_nginx_server_com" | |
// send metrics to statsd | |
client.Inc(st+"."+sn+"."+gs+"."+gt, gv, fi) | |
} | |
func main() { | |
for { | |
// query the NginxStatus function to get data | |
nr, err := NginxStatus() | |
if err != nil { | |
log.Println(err) | |
} | |
// print the statistics on screen | |
fmt.Println("Connections Accepted:", nr.Connections.Accepted) | |
fmt.Println("Connections Dropped:", nr.Connections.Dropped) | |
fmt.Println("Connections Active:", nr.Connections.Active) | |
fmt.Println("Connections Idle", nr.Connections.Idle) | |
// send metrics to the SendStatsD function | |
go SendStatsD("connections", "accepted", nr.Connections.Accepted) | |
go SendStatsD("connections", "dropped", nr.Connections.Dropped) | |
go SendStatsD("connections", "active", nr.Connections.Active) | |
go SendStatsD("connections", "idle", nr.Connections.Idle) | |
// sleep for one second | |
time.Sleep(time.Millisecond * 1000) | |
} | |
} |
Hello! This is a script that interacts with the NGINX Plus live activity monitoring API. It prints data to screen as well as sends the data to graphite. It is an example on how you can approach this type of request with golang, Good luck!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, how are you?
My name is Bruno and I am from Brazil. I am a new programmer, but well even new! Nor I have a college, but I liked the code that you created ... not had time to see what he comes could you explain me?