Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monitoring: Add kubernetes dashboards for cloud tidb cluster #12

Merged
merged 7 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ RUN chmod +x /usr/bin/init.sh

COPY dashboards/*.json /tmp/
COPY rules/*.rules.yml /tmp/
COPY datasources/*.yaml /tmp/

ENTRYPOINT ["/usr/bin/init.sh"]
CMD ["TIDB-Cluster", "/grafana-dashboard-definitions/tidb/", "false", "/etc/prometheus"]
14 changes: 14 additions & 0 deletions cmd/datasource/disk-datasource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"apiVersion": 1,
"datasources": [
{
"access": "proxy",
"editable": true,
"name": "k8s-cluster",
"orgId": 1,
"type": "prometheus",
"url": "http://prometheus-k8s.monitoring.svc:9090",
"version": 1
}
]
}
14 changes: 14 additions & 0 deletions cmd/datasource/tidb-cluster-datasource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"apiVersion": 1,
"datasources": [
{
"access": "proxy",
"editable": false,
"name": "tidb-cluster",
"orgId": 1,
"type": "prometheus",
"url": "http://127.0.0.1:9090",
"version": 1
}
]
}
33 changes: 32 additions & 1 deletion cmd/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,35 @@ cp /tmp/*.rules.yml $PROM_CONFIG_PATH/rules
for file in $PROM_CONFIG_PATH/rules/*
do
sed -i 's/ENV_LABELS_ENV/'$TIDB_CLUSTER_NAME'/g' $file
done
done

# Datasources
if [ $GF_DATASOURCE_PATH ];
then
if [ $GF_NODE_EXPORTER_DATASOURCE_URL ];
qiffang marked this conversation as resolved.
Show resolved Hide resolved
then
sed -i 's,http://prometheus-k8s.monitoring.svc:9090,'$GF_NODE_EXPORTER_DATASOURCE_URL',g' /tmp/disk-datasource.yaml
fi

if [ $GF_TIDB_CLUSTER_URL ];
qiffang marked this conversation as resolved.
Show resolved Hide resolved
then
sed -i 's,http://127.0.0.1:9090,'$GF_TIDB_CLUSTER_URL',g' /tmp/tidb-cluster-datasource.yaml
fi

cp /tmp/disk-datasource.yaml $GF_DATASOURCE_PATH/
cp /tmp/tidb-cluster-datasource.yaml $GF_DATASOURCE_PATH/

# pods
if [ $TIDB_CLUSTER_NAMESPACE ];
then
sed -i 's/TIDB_NAMESPACE/'$TIDB_CLUSTER_NAMESPACE'/g' /tmp/pods.json
fi
sed -i 's/Test-Cluster-Pods/'$TIDB_CLUSTER_NAME'-Pods/g' /tmp/pods.json
cp /tmp/pods.json $GF_PROVISIONING_PATH/dashboards

# nodes
sed -i 's/Test-Cluster-Nodes/'$TIDB_CLUSTER_NAME'-Nodes/g' /tmp/nodes.json
cp /tmp/nodes.json $GF_PROVISIONING_PATH/dashboards

fi

45 changes: 38 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ var (
rules = []string{"tidb.rules.yml", "pd.rules.yml", "tikv-pull.rules.yml", "tikv.rules.yml"}
overviewExlcudeItems = []string{"Services Port Status", "System Info"}
tikvExcludeItems = []string{"IO utilization"}
dockerfiles = []string{"Dockerfile", "init.sh"}
//dockerfiles = []string{"Dockerfile", "init.sh"}

localFiles = map[string]string {
"datasource/disk-datasource.yaml": "datasources",
"datasource/tidb-cluster-datasource.yaml": "datasources",
"pods/pods.json": "dashboards",
"nodes/nodes.json": "dashboards",
"Dockerfile": ".",
"init.sh": ".",
}
)

func main() {
Expand Down Expand Up @@ -100,8 +109,9 @@ func exportMonitorData() {
fetchRules(tag, dir)
return dir
}).Each(func(dir string) {
stream.FromArray(dockerfiles).Each(func(file string) {
copyDockerfiles(baseDir, dir, file)

stream.FromMapEntries(localFiles).Each(func(entry stream.MapEntry) {
copyLocalfiles(baseDir, dir, entry.Key.(reflect.Value).String(), entry.Value.(string))
})
})
}
Expand Down Expand Up @@ -288,10 +298,14 @@ func deleteItem(source string, path string) string {
return newStr
}

func copyDockerfiles(baseDir string, currentDir string, copyFile string) {
df, err := ioutil.ReadFile(fmt.Sprintf("%s%ccmd%c%s", baseDir, filepath.Separator, filepath.Separator, copyFile))
checkErr(err, fmt.Sprintf("read file failed, file=%s", copyFile))
checkErr(ioutil.WriteFile(fmt.Sprintf("%s%c%s", currentDir, filepath.Separator, copyFile), df, os.ModePerm), "create file failed")
func copyLocalfiles(baseDir string, currentDir string, sourceFile string, dstPath string) {
df, err := ioutil.ReadFile(fmt.Sprintf("%s%ccmd%c%s", baseDir, filepath.Separator, filepath.Separator, sourceFile))
checkErr(err, fmt.Sprintf("read file failed, file=%s", sourceFile))
dstDir := fmt.Sprintf("%s%c%s", currentDir, filepath.Separator, dstPath)
if !exist(dstDir) {
os.Mkdir(dstDir, os.ModePerm)
}
checkErr(ioutil.WriteFile(fmt.Sprintf("%s%c%s", dstDir, filepath.Separator, extract(sourceFile)), df, os.ModePerm), "create file failed")
}

func checkErr(err error, msg string) {
Expand All @@ -307,4 +321,21 @@ func compareVersion(tag string) bool {
checkErr(err, "")

return v2.GreaterThanOrEqual(v1)
}

func extract(path string) string {
for i := len(path) - 1; i >= 0; i-- {
if path[i] == filepath.Separator {
return path[i:]
}
}
return path
}

func exist(path string) bool {
if _, err := os.Stat(path); err != nil {
return false
} else {
return true
}
}
Loading