Last active
September 26, 2018 06:29
-
-
Save zhnxin/085b053837661d144e44bfe3e65c00b3 to your computer and use it in GitHub Desktop.
golang to get zabbix graph by itemis with timeout and retry
This file contains 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) 2018 郑欣 <zhngxin@aliyun.com> | |
// This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// This program is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
// Author: 郑欣 <zhngxin@aliyun.com> | |
/* | |
This Package provides a simple client to download graph from zabbix by itemID to jpg image file. | |
Usage: | |
client = zabbix.New("http://127.0.0.1/zabbix","Admin","password") | |
client.SaveImage("73723","") | |
client.SaveImage("73723","~/Pictures/temp.jpg") | |
*/ | |
package zabbix | |
import ( | |
"fmt" | |
"net/http" | |
"os" | |
"path/filepath" | |
"time" | |
"github.com/parnurzeal/gorequest" | |
) | |
var( | |
BaseDir string | |
HTTPRequestTimeOut = time.Duration(5) * time.Second | |
HTTPRequestRetryPeriod = time.Duration(5) * time.Second | |
) | |
func init(){ | |
BaseDir, _ = os.Getwd() | |
} | |
//获取timestamp | |
func getTimeStr() string { | |
timeDelta, _ := time.ParseDuration("-3h") | |
stime := time.Now().Add(timeDelta) | |
timeStr := stime.Format("20060102150405") | |
return timeStr | |
} | |
func generateErr(frefix string, errs []error) error { | |
if errs == nil { | |
return nil | |
} | |
temp := []string{frefix} | |
for _, err := range errs { | |
temp = append(temp, err.Error()) | |
} | |
return fmt.Errorf(strings.Join(temp, ":")) | |
} | |
type Client struct { | |
reqAgent *gorequest.SuperAgent | |
username string | |
password string | |
url string | |
} | |
func New(url, username, password string) *Client { | |
reqAgent := gorequest.New(). | |
SetDoNotClearSuperAgent(true). | |
Timeout(HTTPRequestTimeOut). | |
Retry(3, HTTPRequestRetryPeriod, http.StatusBadRequest, http.StatusInternalServerError) | |
return &Client{ | |
reqAgent: reqAgent, | |
username: username, | |
password: password, | |
url: url, | |
} | |
} | |
func (client *Client) indexUrl() string { | |
return fmt.Sprintf("%s/index.php", client.url) | |
} | |
func (client *Client) login() error { | |
res, _, errs := client.reqAgent.Get(client.indexUrl()). | |
Query("autologin=1&enter=Sign%20in"). | |
Query("name="+client.username). | |
Query("password="+client.password). | |
Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"). | |
AppendHeader("Referer", client.indexUrl()).End() | |
if errs != nil { | |
return generateErr("", errs) | |
} | |
fmt.Println(res.Status) | |
return nil | |
} | |
func (client *Client) SaveImage(itemid, fileName string) (string, error) { | |
if err := client.login(); err != nil { | |
return "", fmt.Errorf("ZabbixClient:SaveImage:GetCookies:%v", err) | |
} | |
timeStr := getTimeStr() | |
_, byteBody, errs := client.reqAgent.Get(client.url + "/chart.php"). | |
Query("period=7200&width=600"). | |
Query("time+" + timeStr). | |
Query("itemids=" + itemid). | |
EndBytes() | |
if errs != nil { | |
return "", generateErr("ZabbixClient:SaveImage:RequestImage", errs) | |
} | |
if fileName == "" { | |
fileName = filepath.Join(BaseDir,"tmp", timeStr+".jpg") | |
} | |
out, err := os.Create(fileName) | |
if err != nil { | |
return fileName, fmt.Errorf("ZabbixClient:SaveImage:CreateImage:%v", err) | |
} | |
defer out.Close() | |
_, err = out.Write(byteBody) | |
if err != nil { | |
return fileName, fmt.Errorf("ZabbixClient:SaveImage:SaveImage:%v", err) | |
} | |
return fileName, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment