Skip to content

Commit

Permalink
Merge pull request #1 from tinglinux/master
Browse files Browse the repository at this point in the history
Add mysql discovery
  • Loading branch information
tinglinux authored Jan 5, 2018
2 parents 416a84c + 9b16ae6 commit 1151335
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
76 changes: 76 additions & 0 deletions exporter/mysql_exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package exporter

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

type NodeInfo struct {
Targets []string `json:"targets"`
Labels Lable `json:"lables"`
}

type Lable struct {
Env string `json:"env"`
Job string `json:"job"`
Loc string `json:"loc"`
Service string `json:"service"`
Tier string `json:"tier"`
}

func DiscoveryAlicloudMysql(filePath string) {
var nodeinfolist []NodeInfo
var nodeinfo NodeInfo

REGIONID := os.Getenv("ALICLOUD_DEFAULT_REGION")
ACCESSKEY := os.Getenv("ALICLOUD_ACCESS_KEY")
SECRETKEY := os.Getenv("ALICLOUD_SECRET_KEY")

ecsClient, err := ecs.NewClientWithAccessKey(
REGIONID,
ACCESSKEY,
SECRETKEY,
)
if err != nil {
panic(err)
}

request := ecs.CreateDescribeInstancesRequest()
request.PageSize = "100"
request.InstanceName = "mysql*"
response, err := ecsClient.DescribeInstances(request)
if err != nil {
panic(err)
}
for _, v := range response.Instances.Instance {
nodeinfo.Targets = append(nodeinfo.Targets, v.InstanceName+":9100")
nodeinfo.Targets = append(nodeinfo.Targets, v.InstanceName+":9104")
for _, y := range v.Tags.Tag {
if y.TagKey == "Env" {
nodeinfo.Labels.Env = y.TagValue
} else if y.TagKey == "Job" {
nodeinfo.Labels.Job = y.TagValue
} else if y.TagKey == "Loc" {
nodeinfo.Labels.Loc = y.TagValue
} else if y.TagKey == "Service" {
nodeinfo.Labels.Service = y.TagValue
} else if y.TagKey == "Tier" {
nodeinfo.Labels.Tier = y.TagValue
}
}
nodeinfolist = append(nodeinfolist, nodeinfo)
nodeinfo.Targets = nil
}
jsonScrapeConfig, err := json.MarshalIndent(nodeinfolist, "", "\t")
if err != nil {
fmt.Println("json err", err)
}
err = ioutil.WriteFile(filePath, jsonScrapeConfig, 0644)
if err != nil {
panic(err)
}
}
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strconv"
"strings"

"github.com/owitho/prometheus-alicloud-sd/exporter"

"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
)
Expand Down Expand Up @@ -154,13 +156,22 @@ func writeSDConfig(scrapeTasks []scrapeTask, output string) {

func main() {
var filePath string
var nodetype string
flag.StringVar(&filePath, "f", "", "Output filename")
flag.StringVar(&nodetype, "t", "", "exporter type(node/mysql)")
flag.Parse()

if filePath == "" {
fmt.Fprintf(os.Stderr, "required arguments -f must pass in.")
os.Exit(1)
}

discoveryAlicloud(filePath)
if nodetype == "node" {
discoveryAlicloud(filePath)
} else if nodetype == "mysql" {
exporter.DiscoveryAlicloudMysql(filePath)
} else if nodetype == "" {
fmt.Fprintf(os.Stderr, "required arguments -t must pass in.")
os.Exit(1)
}
}

0 comments on commit 1151335

Please sign in to comment.