Last active
August 21, 2018 10:24
-
-
Save zhnxin/b25dcc9eea378809c658c7fc99a6b120 to your computer and use it in GitHub Desktop.
golang to get zabbix graph by itemid
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 ( | |
"../../../settings" | |
"../../constatns" | |
"fmt" | |
"io" | |
"net/http" | |
"net/http/cookiejar" | |
"os" | |
"path/filepath" | |
"time" | |
) | |
var( | |
BaseDir string | |
) | |
func init(){ | |
BaseDir, _ = os.Getwd() | |
} | |
func getTimeStr() string { | |
timeDelta, _ := time.ParseDuration("-3h") | |
stime := time.Now().Add(timeDelta) | |
timeStr := stime.Format("20060102150405") | |
return timeStr | |
} | |
type Client struct { | |
client *http.Client | |
username string | |
password string | |
url string | |
} | |
func New(url, username, password string) *Client { | |
jar, _ := cookiejar.New(nil) | |
client := &http.Client{ | |
Timeout: constatns.HttpRequestTimeOut, | |
Jar: jar, | |
} | |
return &Client{ | |
client: client, | |
username: username, | |
password: password, | |
url: url, | |
} | |
} | |
func (client *Client) indexUrl() string { | |
return fmt.Sprintf("%s/index.php", client.url) | |
} | |
func (client *Client) login() error { | |
req, err := http.NewRequest("GET", client.indexUrl(), nil) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
urlQuery := req.URL.Query() | |
urlQuery.Add("request", "charts.php?ddreset=1") | |
urlQuery.Add("name", client.username) | |
urlQuery.Add("password", client.password) | |
urlQuery.Add("autologin", "1") | |
urlQuery.Add("enter", "Sign in") | |
req.URL.RawQuery = urlQuery.Encode() | |
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0") | |
req.Header.Set("Referer", client.indexUrl()) | |
res, err := client.client.Do(req) | |
if err != nil { | |
return err | |
} | |
defer res.Body.Close() | |
return nil | |
} | |
func (client *Client) SaveImage(itemid, fileName string) (string, error) { | |
req, err := http.NewRequest("GET", client.url+"/chart.php", nil) | |
if err != nil { | |
return "", fmt.Errorf("ZabbixClient:SaveImage:GenerateUrl:%v", err) | |
} | |
err = client.login() | |
if err != nil { | |
return "", fmt.Errorf("ZabbixClient:SaveImage:GetCookies:%v", err) | |
} | |
urlQuery := req.URL.Query() | |
urlQuery.Add("itemids", itemid) | |
urlQuery.Add("period", "7200") | |
urlQuery.Add("width", "600") | |
timeStr := getTimeStr() | |
urlQuery.Add("time", timeStr) | |
req.URL.RawQuery = urlQuery.Encode() | |
res, err := client.client.Do(req) | |
if err != nil { | |
return "", fmt.Errorf("ZabbixClient:SaveImage:RequestImage:%v", err) | |
} | |
defer res.Body.Close() | |
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 = io.Copy(out, res.Body) | |
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