Grafana插件開(kāi)發(fā)(一)

1.?什么是Grafana
谷歌一下就知道了 Grafana主頁(yè):Grafana
Github地址:Grafana-Github
2.?如何使用Grafana插件
Grafana插件有四種:
Datasources
Apps
Panels
Dashboards

以Windows系統(tǒng)為例,首先在Grafana首頁(yè)中下載自己想要的Grafana插件, 解壓縮到[$本機(jī)grafana安裝地址]\data\plugins,重啟grafana server, 重新登錄自己的grafana主頁(yè)就可以看到了

3.?開(kāi)發(fā)自己的Grafana插件
Step1

在plugin目錄下新建文件夾,命名為XXX; 在這個(gè)目錄下新建文件plugin.json,用記事本打開(kāi),輸入以下內(nèi)容并保存:
{ "type": "panel", "name": "CHL_Text", "id": "chl-text-panel", "info": { "description": "Text panel made by chl", "author": { "name": "chl", "url": "https://grafana.com" }, "keywords": ["text", "panel"], "logos": { "small": "src/img/icn-text-panel.svg", "large": "src/img/icn-text-panel.svg" }, "version": "0.0.9", "updated": "2017-03-18" }, "dependencies": { "grafanaVersion": "3.x.x", "plugins": [ ] }}
以上內(nèi)容是plugin.json的模板,
第一個(gè)參數(shù)type可以有四個(gè)選項(xiàng),分別對(duì)應(yīng)Grafana的始終插件類(lèi)型;
第二個(gè)參數(shù)指定這個(gè)插件的名稱(chēng);
第三個(gè)參數(shù)為插件指定的唯一的ID,它不能與其他插件的ID相同,約定是 [github username/org]-[plugin name]-[plugin type];
其它都是一些版本信息之類(lèi)的,可以不必關(guān)注。
有了這個(gè)plugin.json文件,重啟grafana服務(wù)器后就能在主頁(yè)上看到自己的插件了。

Step2

要想DashBoard中使用插件,光有plugin.json是不夠的,還需要module.js文件,這是用來(lái)創(chuàng)建Panel實(shí)例的。代碼如下:
/*! grafana - v4.3.2 - 2017-05-31 * Copyright (c) 2017 Torkel ?degaard; Licensed Apache-2.0 */System.register(["lodash","app/plugins/sdk"],function(a,b){ "use strict"; var c,d,e,f=this&&this.__extends||function() { var a=Object.setPrototypeOf|| { proto:[] } instanceof Array&&function(a,b){ a.proto=b } ||function(a,b){ for(var c in b) b.hasOwnProperty(c)&&(a[c]=b[c]) }; return function(b,c){ function d(){ this.constructor=b } a(b,c), b.prototype=null===c?Object.create(c):(d.prototype=c.prototype,new d) } }(); b&&b.id;return{ setters:[function(a){ c=a },function(a){ d=a } ], execute:function(){ e=function(a){ function b(b,d,e,f){ var g=a.call(this,b,d)||this; return g.templateSrv=e,g.$sce=f,g.panelDefaults={ mode:"markdown",content:"# title" }, c.default.defaults(g.panel,g.panelDefaults), g.events.on("init-edit-mode",g.onInitEditMode.bind(g)), g.events.on("refresh",g.onRefresh.bind(g)), g.events.on("render",g.onRender.bind(g)),g } return f(b,a),b.$inject=["$scope","$injector","templateSrv","$sce"], b.prototype.onInitEditMode=function(){ this.addEditorTab("Options","public/app/plugins/panel/text/editor.html"),this.editorTabIndex=1,"text"===this.panel.mode&&(this.panel.mode="markdown") }, b.prototype.onRefresh=function(){ this.render() }, b.prototype.onRender=function(){ "markdown"===this.panel.mode?this.renderMarkdown(this.panel.content):"html"===this.panel.mode&&this.updateContent(this.panel.content),this.renderingCompleted() }, b.prototype.renderText=function(a){ a=a.replace(/&/g,"&").replace(/>/g,">").replace(/</g,"<").replace(/\n/g,"
"), this.updateContent(a) }, b.prototype.renderMarkdown=function(a){ var b=this;return this.remarkable?void this.updateContent(this.remarkable.render(a)):System.import("remarkable").then(function(c){ b.remarkable=new c,b.$scope.$apply(function(){ b.updateContent(b.remarkable.render(a)) }) }) }, b.prototype.updateContent=function(a){ try{ this.content=this.$sce.trustAsHtml(this.templateSrv.replace(a,this.panel.scopedVars)) }catch(b){ console.log("Text panel error: ",b),this.content=this.$sce.trustAsHtml(a) } }, b }(d.PanelCtrl),e.templateUrl="public/app/plugins/panel/text/module.html",a("TextPanelCtrl",e),a("PanelCtrl",e) } }});
重啟grafana,在Dashboard中新建一個(gè)Panel,可以找到我們的CHL_Text,效果如下:


以上就是做一個(gè)最簡(jiǎn)單的插件,不過(guò)module.js我是復(fù)用的別人的代碼,想要做自己的插件,還要去學(xué)習(xí)JS,Angular,任重而道遠(yuǎn)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容