Browse Source

Moving to bash 2

FixSondes
Philippe Caseiro 1 year ago
parent
commit
2de724da57
2 changed files with 78 additions and 33 deletions
  1. 0
    12
      init/prometheus.service
  2. 78
    21
      postservice/88_grafana

+ 0
- 12
init/prometheus.service View File

@@ -1,12 +0,0 @@
1
-[Unit]
2
-Description=Prometheus Server
3
-Documentation=https://prometheus.io/docs/introduction/overview/
4
-After=network-online.target
5
-
6
-[Service]
7
-User=root
8
-Restart=on-failure
9
-ExecStart=/usr/bin/prometheus --config.file=/etc/prometheus/prometheus.yml
10
-
11
-[Install]
12
-WantedBy=multi-user.target

+ 78
- 21
postservice/88_grafana View File

@@ -1,5 +1,78 @@
1 1
 #!/usr/bin/env bash
2 2
 
3
+
4
+function importDataSource()
5
+{
6
+    local grURL="${1}/api/datasources"  # Grafana API URL
7
+    local dsName=${2} # Datasource Name
8
+    local dsURL="${3}" # Datasource URL
9
+    local dsType="prometheus" # Datasource type
10
+
11
+    cmd="curl"
12
+
13
+
14
+    data=$(cat <<__EOF__
15
+{"name":"${dsName}","type":"${dsType}","url":"${dsURL}","access":"direct"}
16
+__EOF__
17
+    )
18
+
19
+    echo -ne "\tCreating datasource for Prometheus "
20
+    res=$(${cmd} "${grURL}" -H "Content-Type: application-json" --data-binary "${data}" 2>&1 )
21
+    excode=${?}
22
+    case $res in
23
+        *"already exists"*)
24
+            echo " ... [Exists]"
25
+            ;;
26
+        *"Datasource added"*)
27
+            echo " ... [OK]"
28
+            ;;
29
+        *)
30
+            echo " ... Ooops ${res}"
31
+            ;;
32
+    esac
33
+}
34
+
35
+function importDashboardFromMarket()
36
+{
37
+    local grURL="${1}/api/dashboards/import" # API URL to create new Dashboard
38
+    local dsName="${2}"                  # Datasource name (to link dashboard to data)
39
+    local dhID="${3}"                    # Dashboard ID in the market
40
+    local dhRev="${4}"                   # Dashboard Revision to download
41
+    local tmpFile=$(mktemp)
42
+    local dhFile=$(mktemp)
43
+
44
+    # URL To download the json file for dashboard
45
+    local pubDashBoardURL="https://grafana.com/api/dashboards/${dhID}/revisions/${dhRev}/download"
46
+
47
+    local cmd="curl"
48
+
49
+    local dh=$(${cmd} --silent ${pubDashBoardURL} 2>&1)
50
+
51
+    cat <<_EOF_ > ${tmpFile}
52
+{
53
+  "inputs": [ { "name":"DS_LOCALHOST", "type":"datasource", "pluginId":"prometheus", "value":"${dsName}" } ],
54
+  "dashboard": { "title": "Surveillance Système", ${dh:1:-1} },
55
+  "folderId": 0,
56
+  "overwrite": true
57
+}
58
+_EOF_
59
+
60
+    res=$(${cmd} "${grURL}" -H "Content-Type: application-json" --data-binary "@${tmpFile}" 2>&1 )
61
+    excode=${?}
62
+    case $res in
63
+        *"\"imported\":true"*)
64
+            echo " ... [Overwrited]"
65
+            ;;
66
+        *"Datasource added"*)
67
+            echo " ... [OK]"
68
+            ;;
69
+        *)
70
+            echo " ... Ooops ${res}"
71
+            ;;
72
+    esac
73
+#    rm -rf ${tmpFile}
74
+}
75
+
3 76
 grafanaHost=$(CreoleGet srvGrafanaIP 192.168.30.57)
4 77
 grafanaPort=$(CreoleGet srvGrafanaPort 3000)
5 78
 grafanaUser="admin"
@@ -8,27 +81,11 @@ grafanaURL="http://${grafanaUser}:${grafanaPasswd}@${grafanaHost}:${grafanaPort}
8 81
 datasource_name=$(CreoleGet promDataSource "prometheus")
9 82
 promHost=$(CreoleGet adresse_ip_eth0)
10 83
 promPort='9090'
84
+datasourceURL="http://${promHost}:${promPort}"
11 85
 
12
-
13
-cmd="curl"
14
-url="${grafanaURL}/api/datasources"
15
-
16
-data=$(cat <<__EOF__
17
-{"name":"${datasource_name}","type":"prometheus","url":"http://${promHost}:${promPort}","access":"direct"}
18
-__EOF__
19
-)
20
-
21
-echo -ne "\tCreating datasource for Prometheus "
22
-res=$(${cmd} "${url}" -H "Content-Type: application-json" --data-binary "${data}" 2>&1 )
23
-excode=${?}
24
-case $res in
25
-    *"already exists"*)
26
-        echo " ... [Exists]"
27
-    ;;
28
-    *"Datasource added"*)
29
-        echo " ... [OK]"
30
-    ;;
31
-esac
32
-
86
+importDataSource ${grafanaURL} ${datasource_name} ${datasourceURL}
87
+echo
33 88
 echo -ne "\tImporting Node Exporter Full Dashboard "
89
+importDashboardFromMarket ${grafanaURL} ${datasource_name} 1860 11
34 90
 
91
+rm -rf ${dashBoardFile}

Loading…
Cancel
Save