flot http://www.e-learn.cn/tag/flot zh-hans flot chart extend lines to end of chart http://www.e-learn.cn/topic/4101261 <span>flot chart extend lines to end of chart</span> <span><span lang="" about="/user/66" typeof="schema:Person" property="schema:name" datatype="">自古美人都是妖i</span></span> <span>2021-02-11 17:47:42</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p></p> <p>I have been trying to extend the "LineChart" in the chart to beginning and the end of the canvas. But i am having no luck with it</p> <p>The options for Flot Charts are .</p> <pre><code>///initialize default chart options chartCtx.options = { grid: { hoverable: true, clickable: true, tickColor: "#d5d5d5", borderWidth: 0, color: '#d5d5d5' }, colors: ["#1ab394", "#464f88"], tooltip: true, xaxis: { mode: "time", timezone: "browser", tickSize: [5, 'minute'], tickLength: 0, axisLabel: "Minutes", axisLabelUseCanvas: true, axisLabelFontSizePixels: 12, axisLabelFontFamily: 'Arial', axisLabelPadding: 10, color: "#d5d5d5" }, yaxes: [ { position: "left", max: 2370, color: "#d5d5d5", axisLabelUseCanvas: true, axisLabelFontSizePixels: 12, axisLabelFontFamily: 'Arial', axisLabelPadding: 3 }, { position: "right", color: "#d5d5d5", axisLabelUseCanvas: true, axisLabelFontSizePixels: 12, axisLabelFontFamily: ' Arial', axisLabelPadding: 67, tickFormatter: function (x) { return x; } } ], legend: { noColumns: 2, labelBoxBorderColor: "#d5d5d5", position: "sw", margin:[0 , -40] } }; //init default chart dataset chartCtx.dataset = [ { label: chartCtx.Bar.Label, data: chartCtx.Bar.Dataset, color: "#1ab394", bars: { show: true, align: "center", barWidth: 5 * 60 * 600, lineWidth:1 } }, { label: chartCtx.Line.Label, data: chartCtx.Line.Dataset, yaxis: 2, color: "#464f88", lines: { lineWidth: 1, show: true, fill: true, fillColor: { colors: [ { opacity: 0.2 }, { opacity: 0.2 } ] } }, points : { show : true , radius : 4 } } ]; </code></pre> <p>Dataset for barGraph is</p> <blockquote> <p>[[1439351830973,464],[1439352130973,574],[1439352430973,177],[1439352730973,784],[1439353030973,902],[1439353330973,2586],[1439353630973,7061],[1439353930973,1091],[1439354230973,2451],[1439354530973,182],[1439354830973,3557]]</p> </blockquote> <p>Dataset for the lineGraph is</p> <blockquote> <p>[[1439351830973,936],[1439352130973,619],[1439352430973,1981],[1439352730973,448],[1439353030973,297],[1439353330973,982],[1439353630973,5606],[1439353930973,5865],[1439354230973,1979],[1439354530973,4495],[1439354830973,2305]]</p> </blockquote> <p>Could you guys point me in the right direction. Thanks..</p> <br /><h3>回答1:</h3><br /><p>You're mapping the line to the bars with the same x-values.</p> <p>You could add two more values to the beginning and end of your line graph array, so it extends beyond the canvas.</p> <pre><code>var buffer = 5 * 60 * 600; // or whatever number to change the timestamp [[1439351830973-buffer,936] // new array element [[1439351830973,936],[1439352130973,619],[1439352430973,1981],[1439352730973,448],[1439353030973,297],[1439353330973,982],[1439353630973,5606],[1439353930973,5865],[1439354230973,1979],[1439354530973,4495],[1439354830973,2305]], [1439354830973+buffer,2305]] // new array element </code></pre> <p>But then the question is, which values to use for the y-values on these new array items? You could use the same value as the penultimate values so the line graph tails off horizontally.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/32063222/flot-chart-extend-lines-to-end-of-chart</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/jquery" hreflang="zh-hans">jquery</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> </div> </div> Thu, 11 Feb 2021 09:47:42 +0000 自古美人都是妖i 4101261 at http://www.e-learn.cn Struts 2 jquery plugin, chart plugin does not support categories http://www.e-learn.cn/topic/4008153 <span>Struts 2 jquery plugin, chart plugin does not support categories</span> <span><span lang="" about="/user/53" typeof="schema:Person" property="schema:name" datatype="">依然范特西╮</span></span> <span>2021-01-24 08:31:54</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>The chart component in the Struts 2 jQuery plugin version 3.6 seems not supporting the <code>category</code> plugin for the <code>sjc:chart</code>.</p> <p>I want to use <code>Flot With “String” x-axis</code> for chart and the best solution is to use <code>jquery.flot.categories.js</code> (Described at Flot With "String" x-axis).</p> <p>As I reviewed some <code>struts-jquery-plugin</code> code, I found that the <code>jquery.chart.struts2.js</code> does not use this plugin, on the other hand the <code>jquery.flot.categories.js</code> file is in the <code>struts2-jquery-chart-plugin-3.7.1.jar</code> file.</p> <p>Please let me know how can I fix it ?! I think I can extend the used jquery objects ( may be <code>$.struts2_jquery_chart</code> ) and add it but don't know how ?!</p> <br /><h3>回答1:</h3><br /><p>It's easily fixable, just modify your <code>jquery.chart.struts2.js</code> and add the following code. </p> <pre class="lang-js prettyprint-override"><code>if ((o.xaxis &amp;&amp; o.xaxis.mode === "categories") || (o.yaxis &amp;&amp; o.yaxis.mode === "categories")) { self.require("js/flot/jquery.flot.categories" + self.minSuffix + ".js"); } </code></pre> <p>If you like to know how to use custom theme with Struts2 jQuery plugin, you should read Overriding Struts2 jqGrid CSS.</p> <p><strong>References:</strong></p> <ul><li>Struts2 jQuery Plugin 3.4.0 with improved Chart Tag released</li> <li>ChartTag</li> <li>Categories</li> </ul><br /><br /><p>来源:<code>https://stackoverflow.com/questions/36673782/struts-2-jquery-plugin-chart-plugin-does-not-support-categories</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/jquery" hreflang="zh-hans">jquery</a></div> <div class="field--item"><a href="/tag/struts2" hreflang="zh-hans">struts2</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> <div class="field--item"><a href="/tag/struts2-jquery" hreflang="zh-hans">struts2-jquery</a></div> <div class="field--item"><a href="/tag/struts2-jquery-chart" hreflang="zh-hans">struts2-jquery-chart</a></div> </div> </div> Sun, 24 Jan 2021 00:31:54 +0000 依然范特西╮ 4008153 at http://www.e-learn.cn Struts 2 jquery plugin, chart plugin does not support categories http://www.e-learn.cn/topic/4008152 <span>Struts 2 jquery plugin, chart plugin does not support categories</span> <span><span lang="" about="/user/173" typeof="schema:Person" property="schema:name" datatype="">孤街浪徒</span></span> <span>2021-01-24 08:31:09</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>The chart component in the Struts 2 jQuery plugin version 3.6 seems not supporting the <code>category</code> plugin for the <code>sjc:chart</code>.</p> <p>I want to use <code>Flot With “String” x-axis</code> for chart and the best solution is to use <code>jquery.flot.categories.js</code> (Described at Flot With "String" x-axis).</p> <p>As I reviewed some <code>struts-jquery-plugin</code> code, I found that the <code>jquery.chart.struts2.js</code> does not use this plugin, on the other hand the <code>jquery.flot.categories.js</code> file is in the <code>struts2-jquery-chart-plugin-3.7.1.jar</code> file.</p> <p>Please let me know how can I fix it ?! I think I can extend the used jquery objects ( may be <code>$.struts2_jquery_chart</code> ) and add it but don't know how ?!</p> <br /><h3>回答1:</h3><br /><p>It's easily fixable, just modify your <code>jquery.chart.struts2.js</code> and add the following code. </p> <pre class="lang-js prettyprint-override"><code>if ((o.xaxis &amp;&amp; o.xaxis.mode === "categories") || (o.yaxis &amp;&amp; o.yaxis.mode === "categories")) { self.require("js/flot/jquery.flot.categories" + self.minSuffix + ".js"); } </code></pre> <p>If you like to know how to use custom theme with Struts2 jQuery plugin, you should read Overriding Struts2 jqGrid CSS.</p> <p><strong>References:</strong></p> <ul><li>Struts2 jQuery Plugin 3.4.0 with improved Chart Tag released</li> <li>ChartTag</li> <li>Categories</li> </ul><br /><br /><p>来源:<code>https://stackoverflow.com/questions/36673782/struts-2-jquery-plugin-chart-plugin-does-not-support-categories</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/jquery" hreflang="zh-hans">jquery</a></div> <div class="field--item"><a href="/tag/struts2" hreflang="zh-hans">struts2</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> <div class="field--item"><a href="/tag/struts2-jquery" hreflang="zh-hans">struts2-jquery</a></div> <div class="field--item"><a href="/tag/struts2-jquery-chart" hreflang="zh-hans">struts2-jquery-chart</a></div> </div> </div> Sun, 24 Jan 2021 00:31:09 +0000 孤街浪徒 4008152 at http://www.e-learn.cn Struts 2 jquery plugin, chart plugin does not support categories http://www.e-learn.cn/topic/4008149 <span>Struts 2 jquery plugin, chart plugin does not support categories</span> <span><span lang="" about="/user/174" typeof="schema:Person" property="schema:name" datatype="">旧街凉风</span></span> <span>2021-01-24 08:30:10</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>The chart component in the Struts 2 jQuery plugin version 3.6 seems not supporting the <code>category</code> plugin for the <code>sjc:chart</code>.</p> <p>I want to use <code>Flot With “String” x-axis</code> for chart and the best solution is to use <code>jquery.flot.categories.js</code> (Described at Flot With "String" x-axis).</p> <p>As I reviewed some <code>struts-jquery-plugin</code> code, I found that the <code>jquery.chart.struts2.js</code> does not use this plugin, on the other hand the <code>jquery.flot.categories.js</code> file is in the <code>struts2-jquery-chart-plugin-3.7.1.jar</code> file.</p> <p>Please let me know how can I fix it ?! I think I can extend the used jquery objects ( may be <code>$.struts2_jquery_chart</code> ) and add it but don't know how ?!</p> <br /><h3>回答1:</h3><br /><p>It's easily fixable, just modify your <code>jquery.chart.struts2.js</code> and add the following code. </p> <pre class="lang-js prettyprint-override"><code>if ((o.xaxis &amp;&amp; o.xaxis.mode === "categories") || (o.yaxis &amp;&amp; o.yaxis.mode === "categories")) { self.require("js/flot/jquery.flot.categories" + self.minSuffix + ".js"); } </code></pre> <p>If you like to know how to use custom theme with Struts2 jQuery plugin, you should read Overriding Struts2 jqGrid CSS.</p> <p><strong>References:</strong></p> <ul><li>Struts2 jQuery Plugin 3.4.0 with improved Chart Tag released</li> <li>ChartTag</li> <li>Categories</li> </ul><br /><br /><p>来源:<code>https://stackoverflow.com/questions/36673782/struts-2-jquery-plugin-chart-plugin-does-not-support-categories</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/jquery" hreflang="zh-hans">jquery</a></div> <div class="field--item"><a href="/tag/struts2" hreflang="zh-hans">struts2</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> <div class="field--item"><a href="/tag/struts2-jquery" hreflang="zh-hans">struts2-jquery</a></div> <div class="field--item"><a href="/tag/struts2-jquery-chart" hreflang="zh-hans">struts2-jquery-chart</a></div> </div> </div> Sun, 24 Jan 2021 00:30:10 +0000 旧街凉风 4008149 at http://www.e-learn.cn 9款好用的bi数据可视化工具 http://www.e-learn.cn/topic/3746542 <span>9款好用的bi数据可视化工具</span> <span><span lang="" about="/user/141" typeof="schema:Person" property="schema:name" datatype="">左心房为你撑大大i</span></span> <span>2020-08-12 06:52:20</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"> <div class="con artical-content editor-preview-side"> <p>大数据、数据分析的兴起和火爆,也带动了数据可视化的广泛应用。说起数据分析和可视化的关系,就好比你为一堆散乱的拼图写了一份说明,告诉他这个数据是什么样子,代表什么。可以说,数据可视化虽然不是必不可少的,但却是可以加快效率,为报告锦上添花的。</p> <p>555.jpg</p> <p>下面分享几款好用的数据可视化工具给大家,以作参考选择:</p> <p>D3<br />D3(Data Driven Documents)是支持SVG渲染的另一种Java库。但是D3能够提供大量线性图和条形图之外的复杂图表样式,例如Voronoi图、树形图、圆形集群和单词云等。D3.js是数据驱动文件(Data-Driven Documents)的缩写,他通过使用HTMLCSS和SVG来渲染精彩的图表和分析图。D3对网页标准的强调足以满足在所有主流浏览器上使用的可能性,使你免于被其他类型架构所捆绑的苦恼,它可以将视觉效果很棒的组件和数据驱动方法结合在一起。<br /></p> <p>DataV<br />阿里出品的数据可视化解决方案,之所以推荐DataV这个后起之秀,完全是因为淘宝双“11”活动中实时互动大屏幕太抢眼了。DataV支持多种数据源,尤其是和阿里系各种数据库完美衔接,如果你的数据本身就存在阿里云上,那选用DataV肯定是个省时省力的好办法。图表方面,DataV内置了丰富的图表模板,支持实时数据采集和解析。<br /></p> <p>Echarts<br />一个纯java的数据可视化库,百度的产品,常应用于软件产品开发或者 系统的图表模块,图表种类多,动态可视化效果,开源免费。Echarts中主要还是以图表为主,没有提供文本和表格方面的展现库,如果有相关需求还需要引入表格和文本方面的其他可视化库。<br /></p> <p>Flot<br />Flot是一个很棒的线图和条形图创建工具,可以运用于支持canvas的所有浏览器——意味着大多数主流浏览器。这是一个jQuery库,如果你已经熟悉jQuery,你就可以容易的对图像进行回调、风格和行为操作。 浮悬的优点是你可以访问大量的调用函数,这样就可以运行你自己的代码。设定一种风格,可以让在用户悬停鼠标、点击、移开鼠标时展示不同的效果。比起其他制图工具,浮悬给予你更多的灵活空间。浮悬提供的选项不多,但它可以很好地执行常见的功能。<br /></p> <p>Tableau<br />Tableau 是桌面系统中最简单的商业智能工具软件,Tableau 没有强迫用户编写自定义代码,新的控制台也可完全自定义配置。在控制台上,不仅能够监测信息,而且还提供完整的分析能力。Tableau控制台灵活,具有高度的动态性。<br /></p> <p>Tableau公司将数据运算与美观的图表完美地嫁接在一起。它的程序很容易上手,各公司可以用它将大量数据拖放到数字“画布”上,转眼间就能创建好各种图表。这一软件的理念是,界面上的数据越容易操控,公司对自己在所在业务领域里的所作所为到底是正确还是错误,就能了解得越透彻。</p> <p>Power BI<br />Power BI 是一套商业分析工具,用于在组织中提供见解。可连接数百个数据源、简化数据准备并提供即席分析。生成美观的报表并进行发布,供组织在 Web 和移动设备上使用。每个人都可创建个性化仪表板,获取针对其业务的全方位独特见解。在企业内实现扩展,内置管理和安全性。<br /></p> <p>Smartbi<br />Smartbi作为成熟的大数据分析平台,具备可复用、 动静结合独特的展示效果,使得数据可视化灵活强大,动静皆宜,为广大用户提供了无限的应用能力和想象空间。<br /></p> <p>除了支持使用Excel作为报表设计器,完美兼容Excel的配置项。支持Excel所有内置图形、背景图、条件格式等设计复杂的仪表盘样式,同时支持完整ECharts 图形库,支持各种各样的图形,包含瀑布图、关系图、雷达图、油量图、热力图、树图等几十种动态交互的图形,借助于地理信息技术,还打造了地图分析功能。</p> <p>Processing<br />Processing 是用 Java 编程语言写的,并且 Java 语言也是在语言树中最接近 Processing 的。所以,如果您熟悉 C 或 Java 语言,Processing 将很容易学。Processing 并不包括 Java 语言的一些较为高级的特性,但这些特性中的很多特性均已集成到了 Processing。如今,围绕它已经形成了一个专门的社区,致力于构建各种库以供用这种语言和环境进行动画、可视化、网络编程以及很多其他的应用。<br /></p> <p>Processing 是一个很棒的进行数据可视化的环境,具有一个简单的接口、一个功能强大的语言以及一套丰富的用于数据以及应用程序导出的机制。</p> <p>Weka<br />Weka是一个能根据属性分类和集群大量数据的优秀工具,Weka不但是数据分析的强大工具,还能生成一些简单的图表。weka首先是一个数据挖掘的利器,它能够快速导入我们的结构化数据,然后对数据属性做分类、聚类分析,帮助我们理解数据。但他的可视化功能同样不逊色,选择界面中的visualization,你会立刻明白:是它让你理解数据,然后你才让用户可视化数据。<br /></p> </div> <div class="alert alert-success" role="alert"><p>来源:<code>oschina</code></p><p>链接:<code>https://my.oschina.net/u/4375893/blog/4411599</code></p></div></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/java" hreflang="zh-hans">java</a></div> <div class="field--item"><a href="/tag/weka" hreflang="zh-hans">weka</a></div> <div class="field--item"><a href="/tag/echarts" hreflang="zh-hans">ECharts</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> <div class="field--item"><a href="/tag/jquery" hreflang="zh-hans">jquery</a></div> <div class="field--item"><a href="/tag/dashuju" hreflang="zh-hans">大数据</a></div> </div> </div> Tue, 11 Aug 2020 22:52:20 +0000 左心房为你撑大大i 3746542 at http://www.e-learn.cn Linear Regression in Javascript http://www.e-learn.cn/topic/3632853 <span>Linear Regression in Javascript</span> <span><span lang="" about="/user/109" typeof="schema:Person" property="schema:name" datatype="">試著忘記壹切</span></span> <span>2020-05-09 18:17:21</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I want to do Least Squares Fitting in Javascript in a web browser.</p> <p>Currently users enter data point information using HTML text inputs and then I grab that data with jQuery and graph it with Flot.</p> <p>After the user had entered in their data points I would like to present them with a "line of best fit". I imagine I would calculate the linear, polynomial, exponential and logarithmic equations and then choose the one with the highest <code>R^2</code> value.</p> <p>I can't seem to find any libraries that will help me to do this though. I stumbled upon jStat, but it is completely missing documentation (as far as I can find) and after digging through the the source code it doesn't seem to have any linear regression functionality built in--I'm basing this purely on function names however.</p> <p>Does anyone know any Javascript libraries that offer simple regression analysis?</p> <hr /><p>The hope would be that I could use the library like so...</p> <p>If I had some set of scatter points in an array <code>var points = [[3,4],[15,45],...[23,78]]</code>, I would be able to hand that to some function like <code>lin_reg(points)</code> and it would return something like <code>[7.12,3]</code> if the linear equation was <code>y = 7.12 x + 3</code>.</p> <br /><h3>回答1:</h3><br /><p>What kind of linear regression? For something simple like least squares, I'd just program it myself:</p> <p>http://mathworld.wolfram.com/LeastSquaresFitting.html</p> <p>The math is not too hard to follow there, give it a shot for an hour or so and let me know if it's too hard, I can try it.</p> <p><strong>EDIT:</strong></p> <p>Found someone that did it:</p> <p>http://dracoblue.net/dev/linear-least-squares-in-javascript/159/</p> <br /><br /><br /><h3>回答2:</h3><br /><p>The simplest solution I found for the question at hand can be found in the following post: http://trentrichardson.com/2010/04/06/compute-linear-regressions-in-javascript/</p> <p>Note that in addition to the linear equation, it also returns the R2 score, which can be useful.</p> <p>** EDIT **</p> <p>Here is the actual code snippet:</p> <pre><code>function linearRegression(y,x){ var lr = {}; var n = y.length; var sum_x = 0; var sum_y = 0; var sum_xy = 0; var sum_xx = 0; var sum_yy = 0; for (var i = 0; i &lt; y.length; i++) { sum_x += x[i]; sum_y += y[i]; sum_xy += (x[i]*y[i]); sum_xx += (x[i]*x[i]); sum_yy += (y[i]*y[i]); } lr['slope'] = (n * sum_xy - sum_x * sum_y) / (n*sum_xx - sum_x * sum_x); lr['intercept'] = (sum_y - lr.slope * sum_x)/n; lr['r2'] = Math.pow((n*sum_xy - sum_x*sum_y)/Math.sqrt((n*sum_xx-sum_x*sum_x)*(n*sum_yy-sum_y*sum_y)),2); return lr; } </code></pre> <p>To use this you just need to pass it two arrays, known_y's and known_x's, so this is what you might pass:</p> <pre><code>var known_y = [1, 2, 3, 4]; var known_x = [5.2, 5.7, 5.0, 4.2]; var lr = linearRegression(known_y, known_x); // now you have: // lr.slope // lr.intercept // lr.r2 </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>I found this great JavaScript library.</p> <p>It's very simple, and seems to work perfectly. </p> <p>I also can't recommend Math.JS enough.</p> <br /><br /><br /><h3>回答4:</h3><br /><p>Check out https://web.archive.org/web/20150523035452/https://cgwb.nci.nih.gov/cgwbreg.html (javascript regression calculator) - pure JavaScript, not CGI calls to server. The data and processing remains on your computer. Complete R style results and R code to check the work and a visualization of the results.</p> <p>See the source code for the embedded JavaScript implementations of OLS and statistics associated with the results.</p> <p>The code is my effort to port the GSL library functions to JavaScript. </p> <p>The codes is released under GPL because it's basically line for line porting of GPL licensed Gnu Scientific Library (GSL) code.</p> <p>EDIT: Paul Lutus also provides some GPL code for regression at: http://arachnoid.com/polysolve/index.html</p> <br /><br /><br /><h3>回答5:</h3><br /><p>Simple linear regression with measures of variation ( Total sum of squares = Regression sum of squares + Error sum of squares ), Standard error of estimate SEE (Residual standard error), and coefficients of determination R2 and correlation R.</p> <pre><code>const regress = (x, y) =&gt; { const n = y.length; let sx = 0; let sy = 0; let sxy = 0; let sxx = 0; let syy = 0; for (let i = 0; i &lt; n; i++) { sx += x[i]; sy += y[i]; sxy += x[i] * y[i]; sxx += x[i] * x[i]; syy += y[i] * y[i]; } const mx = sx / n; const my = sy / n; const yy = n * syy - sy * sy; const xx = n * sxx - sx * sx; const xy = n * sxy - sx * sy; const slope = xy / xx; const intercept = my - slope * mx; const r = xy / Math.sqrt(xx * yy); const r2 = Math.pow(r,2); let sst = 0; for (let i = 0; i &lt; n; i++) { sst += Math.pow((y[i] - my), 2); } const sse = sst - r2 * sst; const see = Math.sqrt(sse / (n - 2)); const ssr = sst - sse; return {slope, intercept, r, r2, sse, ssr, sst, sy, sx, see}; } regress([1, 2, 3, 4, 5], [1, 2, 3, 4, 3]); </code></pre> <br /><br /><br /><h3>回答6:</h3><br /><p>Here is a snippet that will take an array of triplets (x, y, r) where r is the weight of the (x, y) data point and return [a, b] such that Y = a*X + b approximate the data.</p> <pre><code>// return (a, b) that minimize // sum_i r_i * (a*x_i+b - y_i)^2 function linear_regression( xyr ) { var i, x, y, r, sumx=0, sumy=0, sumx2=0, sumy2=0, sumxy=0, sumr=0, a, b; for(i=0;i&lt;xyr.length;i++) { // this is our data pair x = xyr[i][0]; y = xyr[i][1]; // this is the weight for that pair // set to 1 (and simplify code accordingly, ie, sumr becomes xy.length) if weighting is not needed r = xyr[i][2]; // consider checking for NaN in the x, y and r variables here // (add a continue statement in that case) sumr += r; sumx += r*x; sumx2 += r*(x*x); sumy += r*y; sumy2 += r*(y*y); sumxy += r*(x*y); } // note: the denominator is the variance of the random variable X // the only case when it is 0 is the degenerate case X==constant b = (sumy*sumx2 - sumx*sumxy)/(sumr*sumx2-sumx*sumx); a = (sumr*sumxy - sumx*sumy)/(sumr*sumx2-sumx*sumx); return [a, b]; } </code></pre> <br /><br /><br /><h3>回答7:</h3><br /><p>Somewhat based on Nic Mabon's answer.</p> <pre><code>function linearRegression(x, y) { var xs = 0; // sum(x) var ys = 0; // sum(y) var xxs = 0; // sum(x*x) var xys = 0; // sum(x*y) var yys = 0; // sum(y*y) var n = 0; for (; n &lt; x.length &amp;&amp; n &lt; y.length; n++) { xs += x[n]; ys += y[n]; xxs += x[n] * x[n]; xys += x[n] * y[n]; yys += y[n] * y[n]; } var div = n * xxs - xs * xs; var gain = (n * xys - xs * ys) / div; var offset = (ys * xxs - xs * xys) / div; var correlation = Math.abs((xys * n - xs * ys) / Math.sqrt((xxs * n - xs * xs) * (yys * n - ys * ys))); return { gain: gain, offset: offset, correlation: correlation }; } </code></pre> <p>Then y' = x * gain + offset.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/6195335/linear-regression-in-javascript</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/jquery" hreflang="zh-hans">jquery</a></div> <div class="field--item"><a href="/tag/statistics" hreflang="zh-hans">statistics</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> <div class="field--item"><a href="/tag/linear-regression" hreflang="zh-hans">linear-regression</a></div> </div> </div> Sat, 09 May 2020 10:17:21 +0000 試著忘記壹切 3632853 at http://www.e-learn.cn Prometheus监控 Redis & Redis Cluster 说明 http://www.e-learn.cn/topic/3590067 <span>Prometheus监控 Redis &amp; Redis Cluster 说明</span> <span><span lang="" about="/user/40" typeof="schema:Person" property="schema:name" datatype="">こ雲淡風輕ζ</span></span> <span>2020-04-24 08:19:22</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"> <span id="OSC_h2_1"></span> <h2>说明</h2> <div class="postBody"> <div class="blogpost-body"> <p><span style="font-size: 15px;">在前面的<a href="https://www.cnblogs.com/zhoujinyi/p/12012398.html" rel="nofollow">Prometheus + Grafana 部署说明之「安装」</a>文章里,大致介绍说明了<a href="https://prometheus.io/" target="_blank" rel="nofollow">Prometheus</a>和<a href="https://grafana.com/" target="_blank" rel="nofollow">Grafana</a>的一些安装使用,现在开始如何始部署<a href="https://prometheus.io/" target="_blank" rel="nofollow">Prometheus</a>+<a href="https://grafana.com/" target="_blank" rel="nofollow">Grafana</a> 来监控<a href="https://github.com/oliver006/redis_exporter" target="_blank" rel="nofollow">Redis</a></span></p> <span id="OSC_h2_2"></span> <h2>部署</h2> <p><span><span style="font-size: 15px;">在之前介绍的<a href="https://www.cnblogs.com/zhoujinyi/p/12012398.html" rel="nofollow">Prometheus</a>系列文章里,已经了解了<a href="https://www.cnblogs.com/zhoujinyi/p/12012398.html" rel="nofollow">Prometheus</a>是通过拉取的方式来保存监控数据的,可以自己写一个服务来去Redis来获取需要监控的相关指标,也可以直接用现成的exporter,如GitHub上<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a>,本文对Redis的监控是通过该导出器进行的。</span><br /></span></p> <span id="OSC_h3_3"></span> <h3><span><span style="font-size: 15px;"><a href="https://github.com/oliver006/redis_exporter/releases" target="_blank" rel="nofollow">下载</a>&amp;解压:</span></span></h3> <div class="cnblogs_code"> <pre><code><span style="color: #0000ff;">#下载<br />wget<br /></span> https:<span style="color: #008000;">//</span><span style="color: #008000;">github.com/oliver006/redis_exporter/releases/download/v1.3.5/redis_exporter-v1.3.5.linux-amd64.tar.gz</span><br />#解压<br /><span style="color: #0000ff;">tar</span> xvf redis_exporter-v1.<span style="color: #800080;">3.5</span>.linux-amd64.<span style="color: #0000ff;">tar</span>.gz<br /><br /></code></pre> </div> <p><span style="font-size: 15px;">注意:</span><span style="font-size: 15px;"><a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a> 版本从 <strong>0.x to 1.x </strong>有了很大的改变:为了使redis_exporter更紧密地遵循“编写导出器的最佳实践”,</span><strong><span style="font-size: 15px;">不再直接支持配置(和抓取)多个Redis实例,而是将这一任务推给了Prometheus服务器,</span></strong><span style="font-size: 15px;">具体情况可以看<a href="https://github.com/oliver006/redis_exporter/issues/278" target="_blank" rel="nofollow">作者说明</a>。如果需要抓取多个实例</span><span style="font-size: 15px;">,请查看<a href="https://github.com/oliver006/redis_exporter#prometheus-configuration-to-scrape-multiple-redis-hosts" target="_blank" rel="nofollow">如何配置Prometheus服务器以刮取多个实例</a>。</span></p> <p><span style="font-size: 15px;">但如果已经按照Prometheus项目的推荐使用了导出器,并且仅scrape了一个实例(例如使用--redis.addr和--redis.password 标志),那么没有任何改变。</span></p> <span id="OSC_h3_4"></span> <h3>准备:</h3> <p><span style="font-size: 15px;">1)在开启<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a> [<strong><a href="https://github.com/oliver006/redis_exporter/releases/tag/v1.3.5" rel="nofollow">v1.3.5</a></strong>]之前,需要先配置<a href="https://www.cnblogs.com/zhoujinyi/p/12012398.html" rel="nofollow">Prometheus</a>,按照<a href="https://www.cnblogs.com/zhoujinyi/p/12012398.html" rel="nofollow">Prometheus + Grafana 部署说明之「安装」</a>装好Prometheus之后,修改其配置文件。</span><span style="font-size: 15px;"><span>本次示例是通过文件服务发现来处理的,因为监控服务的同时也要监控系统相关的指标,所以需要<span>node_exporter和<span>redis_exporter。文件发现机制有2种方式:exporter配置可以放到一个文件,也可以为每个实例单独配置文件。如放到一个配置文件:</span></span></span></span></p> <p><span style="font-size: 15px;">① <strong>多实例相同密码</strong>的抓取(只需要一个redis-exporter),集群信息:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;">3节点无从,密码 abc 192.168.163.132、133、134 -- 6379 192.168.163.132、133、134 -- 6380</span></code></pre> </div> <p><span style="font-size: 15px;"><strong>1.1 exporter配置放到一个文件的相关配置:</strong><br /></span></p> <p><strong><span style="font-size: 15px;">prometheus.yml:</span></strong></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">global: scrape_interval: 10s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 10s # Evaluate rules every 15 seconds. The default is every 1 minute. alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 rule_files: # - "first_rules.yml" # - "second_rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.163.132:9099'] - job_name: 'node_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/node/nodes-instances.json refresh_interval: 10s relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostname - action: labeldrop regex: __meta_filepath - job_name: 'redis_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/redis/redis-instances.json metrics_path: /scrape relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostip - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 192.168.163.132:9123</span></code></pre> </div> <span>View Code</span> </div> <p><strong><span style="font-size: 15px;">nodes-instances.json:</span></strong></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">[ { "targets": [ "192.168.163.132:9199", "192.168.163.133:9199","192.168.163.134:9199"] } ]</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;"><strong><span>redis-instances.json:</span></strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">[ { "targets": [ "192.168.163.132:6379","192.168.163.133:6379","192.168.163.134:6379"], "labels": {"env":"test","cluster":"test_one"} }, { "targets": [ "192.168.163.132:6380","192.168.163.133:6380","192.168.163.134:6380"], "labels": {"env":"test","cluster":"test_two"} } ]</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">以后如果有相关的实例集群包括主从加进来,只要修改以上2个exporter文件即可。</span><span style="font-size: 15px;">最后开启exporter:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;">#各个节点执行 node-exporter: ./node_exporter --web.listen-address=:9199 #指定节点执行 redis_exporter: ./redis_exporter -redis-only-metrics -redis.password abc -web.listen-address=:9123</span></code></pre> </div> <p><strong><span style="font-size: 15px;">1.2 exporter配置独立存放的相关配置:</span></strong></p> <p><span style="font-size: 15px;"><strong>prometheus.yml:</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">global: scrape_interval: 10s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 10s # Evaluate rules every 15 seconds. The default is every 1 minute. alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 rule_files: # - "first_rules.yml" # - "second_rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.163.132:9099'] - job_name: 'node_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/node/*.yml refresh_interval: 10s relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostname - action: labeldrop regex: __meta_filepath - job_name: 'redis_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/redis/*.yml metrics_path: /scrape relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostip - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 192.168.163.132:9123</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;"><strong><span>targets/node/*.yml:</span></strong></span> </p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;"># ls -lh /etc/prometheus/targets/node/*.yml -rw-r--r-- 1 root root 117 4月 18 14:51 /etc/prometheus/targets/node/test2_node_exporter.yml -rw-r--r-- 1 root root 117 4月 18 14:51 /etc/prometheus/targets/node/test3_node_exporter.yml -rw-r--r-- 1 root root 117 4月 18 14:51 /etc/prometheus/targets/node/test4_node_exporter.yml # cat /etc/prometheus/targets/node/test2_node_exporter.yml [ { "targets": ["192.168.163.132:9199"], "labels": {"env":"test","alias":"test2"} } ] # cat /etc/prometheus/targets/node/test3_node_exporter.yml [ { "targets": ["192.168.163.133:9199"], "labels": {"env":"test","alias":"test3"} } ] # cat /etc/prometheus/targets/node/test4_node_exporter.yml [ { "targets": ["192.168.163.134:9199"], "labels": {"env":"test","alias":"test4"} } ]</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;"><strong><span>targets/redis/*.yml:</span></strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;"># ls -lh /etc/prometheus/targets/redis/*.yml -rw-r--r-- 1 root root 121 4月 18 13:14 /etc/prometheus/targets/redis/test2_6379_redis_exporter.yml -rw-r--r-- 1 root root 121 4月 18 13:14 /etc/prometheus/targets/redis/test2_6380_redis_exporter.yml -rw-r--r-- 1 root root 121 4月 18 13:14 /etc/prometheus/targets/redis/test3_6379_redis_exporter.yml -rw-r--r-- 1 root root 121 4月 18 13:15 /etc/prometheus/targets/redis/test3_6380_redis_exporter.yml -rw-r--r-- 1 root root 121 4月 18 13:14 /etc/prometheus/targets/redis/test4_6379_redis_exporter.yml -rw-r--r-- 1 root root 121 4月 18 13:15 /etc/prometheus/targets/redis/test4_6380_redis_exporter.yml # cat /etc/prometheus/targets/redis/test2_6379_redis_exporter.yml [ { "targets": [ "192.168.163.132:6379"], "labels": {"env":"test","cluster":"test_one","alias":"test2"} } ] # cat /etc/prometheus/targets/redis/test3_6379_redis_exporter.yml [ { "targets": [ "192.168.163.133:6379"], "labels": {"env":"test","cluster":"test_one","alias":"test3"} } ] # cat /etc/prometheus/targets/redis/test4_6379_redis_exporter.yml [ { "targets": [ "192.168.163.134:6379"], "labels": {"env":"test","cluster":"test_one","alias":"test4"} } ] # cat /etc/prometheus/targets/redis/test2_6380_redis_exporter.yml [ { "targets": [ "192.168.163.132:6380"], "labels": {"env":"test","cluster":"test_two","alias":"test2"} } ] # cat /etc/prometheus/targets/redis/test3_6380_redis_exporter.yml [ { "targets": [ "192.168.163.133:6380"], "labels": {"env":"test","cluster":"test_two","alias":"test3"} } ] # cat /etc/prometheus/targets/redis/test4_6380_redis_exporter.yml [ { "targets": [ "192.168.163.134:6380"], "labels": {"env":"test","cluster":"test_two","alias":"test4"} } ]</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">以后如果有相关的实例集群加进来包括主从,只要新增2类exporter文件即可。最后开启exporter:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;">#各个节点执行 node-exporter: ./node_exporter --web.listen-address=:9199 #指定节点执行 redis_exporter: ./redis_exporter -redis-only-metrics -redis.password abc -web.listen-address=:9123</span></code></pre> </div> <p><span style="font-size: 15px;"><strong>以上2种通过文件服务发现的方法,推荐第2个。因为新增的文件有问题也不影响服务,修改文件可能会引发问题;并且也可以为各个节点打需要的labels。</strong></span></p> <p><span style="font-size: 15px;">最后配置Grafana,效果:</span></p> <p><span style="font-size: 15px;"><img alt="" width="832" height="346" class="b-lazy" data-src="https://img2020.cnblogs.com/blog/163084/202004/163084-20200418151825988-1639689149.png" data-original="https://img2020.cnblogs.com/blog/163084/202004/163084-20200418151825988-1639689149.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /></span></p> <p><strong><span style="font-size: 15px;">JSON Model:</span></strong></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">{ "annotations": { "list": [ { "builtIn": 1, "datasource": "-- Grafana --", "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", "name": "Annotations &amp; Alerts", "type": "dashboard" } ] }, "editable": true, "gnetId": null, "graphTooltip": 0, "id": 13, "iteration": 1587194136640, "links": [], "panels": [ { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群个数", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 0, "y": 0 }, "id": 8, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(count(redis_up{cluster=~\"$cluster\"}&gt;0) by (cluster))", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 5, "y": 0 }, "id": 4, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_up{cluster=~\"$cluster\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群活跃节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 4, "x": 10, "y": 0 }, "id": 6, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_up{cluster=~\"$cluster\"}&gt;0)", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群活跃节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorPrefix": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "主节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 14, "y": 0 }, "id": 10, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{cluster=~\"$cluster\", role=\"master\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "主节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "从节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 19, "y": 0 }, "id": 12, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{cluster=~\"$cluster\", role=\"slave\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "从节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redus Cluster OPS指标", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 3 }, "hiddenSeries": false, "id": 16, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "ceil(sum(rate(redis_commands_processed_total{cluster=~\"$cluster\",instance=~\"$master\"}[$interval])) by (cluster))", "interval": "5s", "legendFormat": "{{cluster}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis Cluster OPS", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用大小值", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 3 }, "hiddenSeries": false, "id": 18, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_memory_used_bytes{instance=~\"$instance\"} ", "interval": "5s", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用大小", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "bytes", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis Server CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 11 }, "hiddenSeries": false, "id": 14, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "100* (rate(redis_cpu_sys_seconds_total{instance=~\"$instance\"}[$interval]) + rate(redis_cpu_user_seconds_total{instance=~\"$instance\"}[$interval]))", "interval": "5s", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 11 }, "hiddenSeries": false, "id": 20, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "(redis_memory_used_bytes{instance=~\"$instance\"} / redis_config_maxmemory{instance=~\"$instance\"}) * 100", "interval": "5s", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 19 }, "hiddenSeries": false, "id": 24, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "topk(3,clamp_max((avg by (hostname,mode) ((clamp_max(rate(node_cpu_seconds_total{hostname=~\"$host\",mode!=\"idle\"}[$interval]),1)) or (clamp_max(irate(node_cpu_seconds_total{hostname=~\"$host\",mode!=\"idle\"}[5m]),1)) ))*100,100))", "hide": true, "interval": "", "legendFormat": "{{mode}}", "refId": "A" }, { "expr": "1 - avg by (hostname)(rate(node_cpu_seconds_total{hostname=~\"$host\", mode=\"idle\"}[$interval]))", "interval": "", "legendFormat": "{{hostname}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percentunit", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统物理内存利用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 19 }, "hiddenSeries": false, "id": 30, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "(node_memory_Cached_bytes{hostname=~\"$host\"} + node_memory_Buffers_bytes{hostname=~\"$host\"} + node_memory_MemFree_bytes{hostname=~\"$host\"})/node_memory_MemTotal_bytes{hostname=~\"$host\"}", "hide": true, "interval": "", "legendFormat": "{{hostname}}", "refId": "A" }, { "expr": "((node_memory_MemTotal_bytes{hostname=~\"$host\"} - (node_memory_MemAvailable_bytes{hostname=~\"$host\"} or (node_memory_MemFree_bytes{hostname=~\"$host\"} + node_memory_Buffers_bytes{hostname=~\"$host\"} + node_memory_Cached_bytes{hostname=~\"$host\"})))*100 / node_memory_MemTotal_bytes{hostname=~\"$host\"})", "hide": false, "interval": "", "legendFormat": "{{hostname}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统物理内存利用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": true, "cacheTimeout": null, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统磁盘使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 27 }, "hiddenSeries": false, "id": 32, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": false, "linewidth": 1, "links": [], "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pluginVersion": "6.7.2", "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "topk(10,max((1 - node_filesystem_avail_bytes{hostname=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"} / node_filesystem_size_bytes{hostname=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"})*100) by (hostname) &gt; 0)", "interval": "", "legendFormat": "{{hostname}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统磁盘使用率", "tooltip": { "shared": false, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "series", "name": null, "show": true, "values": [ "current" ] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点以及集群的 KEYS 总数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 27 }, "hiddenSeries": false, "id": 22, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "sum(redis_db_keys{instance=~\"$master\"}) by (instance,db)", "interval": "", "legendFormat": "{{instance}}", "refId": "A" }, { "expr": "sum(redis_db_keys{instance=~\"$master\"}) by (cluster)", "interval": "", "legendFormat": "{{cluster}}_total", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "KEYS 总数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点活跃连接数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 35 }, "hiddenSeries": false, "id": 26, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_connected_clients{instance=~\"$instance\"}", "interval": "", "legendFormat": "{{hostip}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "活跃连接数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点内存碎片率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 35 }, "hiddenSeries": false, "id": 28, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_mem_fragmentation_ratio{instance=~\"$instance\"}", "interval": "", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "内存碎片率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点 的负载", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 43 }, "hiddenSeries": false, "id": 34, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "node_load1{hostname=~\"$host\"}", "interval": "", "legendFormat": "{{hostname}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "负载(Load)", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } } ], "refresh": "5s", "schemaVersion": 22, "style": "dark", "tags": [], "templating": { "list": [ { "auto": true, "auto_count": 100, "auto_min": "1s", "current": { "selected": false, "text": "5s", "value": "5s" }, "hide": 0, "label": "interval", "name": "interval", "options": [ { "selected": false, "text": "auto", "value": "$__auto_interval_interval" }, { "selected": true, "text": "5s", "value": "5s" }, { "selected": false, "text": "30s", "value": "30s" }, { "selected": false, "text": "1m", "value": "1m" }, { "selected": false, "text": "10m", "value": "10m" }, { "selected": false, "text": "30m", "value": "30m" }, { "selected": false, "text": "1h", "value": "1h" }, { "selected": false, "text": "6h", "value": "6h" }, { "selected": false, "text": "12h", "value": "12h" }, { "selected": false, "text": "1d", "value": "1d" } ], "query": "5s,30s,1m,10m,30m,1h,6h,12h,1d", "refresh": 2, "skipUrlSync": false, "type": "interval" }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_up,cluster)", "hide": 0, "includeAll": true, "index": -1, "label": "Cluster", "multi": true, "name": "cluster", "options": [], "query": "label_values(redis_up,cluster)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info,role)", "hide": 0, "includeAll": true, "index": -1, "label": "Role", "multi": true, "name": "role", "options": [], "query": "label_values(redis_instance_info,role)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info{cluster=~\"$cluster\",role=~\"$role\"},instance)", "hide": 2, "includeAll": true, "index": -1, "label": "instance", "multi": true, "name": "instance", "options": [], "query": "label_values(redis_instance_info{cluster=~\"$cluster\",role=~\"$role\"},instance)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info{cluster=~\"$cluster\",role=\"master\"},instance)", "hide": 2, "includeAll": true, "index": -1, "label": "master", "multi": true, "name": "master", "options": [], "query": "label_values(redis_instance_info{cluster=~\"$cluster\",role=\"master\"},instance)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_up{cluster=~\"$cluster\"}, hostip)", "hide": 2, "includeAll": true, "index": -1, "label": "host", "multi": true, "name": "host", "options": [], "query": "label_values(redis_up{cluster=~\"$cluster\"}, hostip)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false } ] }, "time": { "from": "now-5m", "to": "now" }, "timepicker": { "refresh_intervals": [ "5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d" ] }, "timezone": "", "title": "Redis_Cluster_V1.35", "uid": "5FfBHG3Zz", "variables": { "list": [] }, "version": 3 }</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">②:<strong>不同密码多实例</strong>抓取,因为使用的是 &gt;<strong>1.x 的<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a> [<a href="https://github.com/oliver006/redis_exporter/releases/tag/v1.3.5" rel="nofollow">v1.3.5</a>],</strong>不支持多实例。因为一个export只能指定一个密码,如果想进行多实例不同密码抓取的话,所以需要在Prometheus配置多个<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a>,集群信息为:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;">3节点无从 192.168.163.132、133、134 -- 6379 密码 abc 192.168.163.132、133、134 -- 6380 密码 cba</span></code></pre> </div> <p><span style="font-size: 15px;"><strong>prometheus.yml:</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">global: scrape_interval: 10s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 10s # Evaluate rules every 15 seconds. The default is every 1 minute. alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 rule_files: # - "first_rules.yml" # - "second_rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.163.132:9099'] - job_name: 'node_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/node/*.yml refresh_interval: 10s relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostname - action: labeldrop regex: __meta_filepath - job_name: 'redis_exporter_A' scrape_interval: 1s file_sd_configs: - files: - targets/redis/A*.yml metrics_path: /scrape relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostip - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 192.168.163.132:9123 - job_name: 'redis_exporter_B' scrape_interval: 1s file_sd_configs: - files: - targets/redis/B*.yml metrics_path: /scrape relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostip - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 192.168.163.132:9124</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;"><strong><span>targets/redis/*.yml:</span></strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;"># ls -lh /etc/prometheus/targets/redis/A*.yml -rw-r--r-- 1 root root 121 4月 18 13:14 /etc/prometheus/targets/redis/Atest2_6379_redis_exporter.yml -rw-r--r-- 1 root root 121 4月 18 13:14 /etc/prometheus/targets/redis/Atest3_6379_redis_exporter.yml -rw-r--r-- 1 root root 121 4月 18 13:14 /etc/prometheus/targets/redis/Atest4_6379_redis_exporter.yml # ls -lh /etc/prometheus/targets/redis/B*.yml -rw-r--r-- 1 root root 121 4月 18 13:14 /etc/prometheus/targets/redis/Btest2_6380_redis_exporter.yml -rw-r--r-- 1 root root 121 4月 18 13:15 /etc/prometheus/targets/redis/Btest3_6380_redis_exporter.yml -rw-r--r-- 1 root root 121 4月 18 13:15 /etc/prometheus/targets/redis/Btest4_6380_redis_exporter.yml # cat /etc/prometheus/targets/redis/Atest2_6379_redis_exporter.yml [ { "targets": [ "192.168.163.132:6379"], "labels": {"env":"test","cluster":"test_one","alias":"test2"} } ] # cat /etc/prometheus/targets/redis/Atest3_6379_redis_exporter.yml [ { "targets": [ "192.168.163.133:6379"], "labels": {"env":"test","cluster":"test_one","alias":"test3"} } ] # cat /etc/prometheus/targets/redis/Atest4_6379_redis_exporter.yml [ { "targets": [ "192.168.163.134:6379"], "labels": {"env":"test","cluster":"test_one","alias":"test4"} } ] # cat /etc/prometheus/targets/redis/Btest2_6380_redis_exporter.yml [ { "targets": [ "192.168.163.132:6380"], "labels": {"env":"test","cluster":"test_two","alias":"test2"} } ] # cat /etc/prometheus/targets/redis/Btest3_6380_redis_exporter.yml [ { "targets": [ "192.168.163.133:6380"], "labels": {"env":"test","cluster":"test_two","alias":"test3"} } ] # cat /etc/prometheus/targets/redis/Btest4_6380_redis_exporter.yml [ { "targets": [ "192.168.163.134:6380"], "labels": {"env":"test","cluster":"test_two","alias":"test4"} } ]</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">以后如果有相关的实例集群包括主从加进来,只要新增2类exporter文件即可。最后开启exporter:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;">#指定节点执行 redis_exporter: ./redis_exporter -redis-only-metrics -redis.password abc -web.listen-address=:9123 #指定节点执行 redis_exporter: ./redis_exporter -redis-only-metrics -redis.password cba -web.listen-address=:9124</span></code></pre> </div> <p><span style="font-size: 15px;">最后配置Grafana,效果和①一样,唯一的区别就是②开多个exporter。这样会导致一个问题:每次增加不同密码的节点都会修改Prometheus配置文件,既不安全也不方便。可以通过下面方法来实现多实例不同密码抓取。按照编写导出器的最佳实践,需要像nodes_exporter一样在不同实例的主机上安装和开启<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a>,即为每个redis instance开启一个exporter。集群信息为:<br /></span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;">集群1:密码 cc<br />M: 192.168.163.132:15389 192.168.163.133:15389 192.168.163.134:15389 S: 192.168.163.132:15489 192.168.163.133:15489 192.168.163.134:15489 集群2:密码abc M: 192.168.163.132:6379 192.168.163.133:6379 192.168.163.134:6379 S: 192.168.163.132:6381 192.168.163.133:6381 192.168.163.134:6381<br /><br /></span></code></pre> </div> <p><span style="font-size: 15px;"><strong>prometheus.yml:</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">global: scrape_interval: 10s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 10s # Evaluate rules every 15 seconds. The default is every 1 minute. alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 rule_files: # - "first_rules.yml" # - "second_rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.163.132:9099'] - job_name: 'node_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/node/node*.yml refresh_interval: 10s relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostname - action: labeldrop regex: __meta_filepath - job_name: 'redis_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/redis/redis*.yml relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostip - action: labeldrop regex: __meta_filepath</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;"><strong><span>targets/redis/redis*.yml</span></strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">root@test2:/etc/prometheus/targets/redis# ls -lh redis* -rw-r--r-- 1 prometheus prometheus 117 4月 18 22:02 redis_test2_15389_exporter.yml -rw-r--r-- 1 prometheus prometheus 117 4月 18 22:05 redis_test2_15489_exporter.yml -rw-r--r-- 1 prometheus prometheus 117 4月 18 22:02 redis_test3_15389_exporter.yml -rw-r--r-- 1 prometheus prometheus 117 4月 18 22:05 redis_test3_15489_exporter.yml -rw-r--r-- 1 prometheus prometheus 117 4月 18 22:03 redis_test4_15389_exporter.yml -rw-r--r-- 1 prometheus prometheus 117 4月 18 22:05 redis_test4_15489_exporter.yml root@test2:/etc/prometheus/targets/redis# cat redis_test2_15389_exporter.yml [ { "targets": ["192.168.163.132:9121"], "labels": {"env":"test","cluster":"test","alias":"test2"} } ] root@test2:/etc/prometheus/targets/redis# cat redis_test3_15389_exporter.yml [ { "targets": ["192.168.163.133:9121"], "labels": {"env":"test","cluster":"test","alias":"test3"} } ] root@test2:/etc/prometheus/targets/redis# cat redis_test4_15389_exporter.yml [ { "targets": ["192.168.163.134:9121"], "labels": {"env":"test","cluster":"test","alias":"test4"} } ] root@test2:/etc/prometheus/targets/redis# cat redis_test2_15489_exporter.yml [ { "targets": ["192.168.163.132:9122"], "labels": {"env":"test","cluster":"test","alias":"test2"} } ] root@test2:/etc/prometheus/targets/redis# cat redis_test3_15489_exporter.yml [ { "targets": ["192.168.163.133:9122"], "labels": {"env":"test","cluster":"test","alias":"test3"} } ] root@test2:/etc/prometheus/targets/redis# cat redis_test4_15489_exporter.yml [ { "targets": ["192.168.163.134:9122"], "labels": {"env":"test","cluster":"test","alias":"test4"} } ]</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">通过文件服务发现,实现动态添加节点(exporter),一个Redis实例一个exporter,这里只开集群1的exporter,即在test2、test3、test4上各开启2个exporter,共6个exporter对应6个Redis实例,配置完成之后,可以直接到各个监控主机上启动<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a>:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;"><strong>test2:</strong> ./redis_exporter -redis-only-metrics -redis.addr 192.168.163.132:15389 -redis.password cc -web.listen-address=:9121 ./redis_exporter -redis-only-metrics -redis.addr 192.168.163.132:15489 -redis.password cc -web.listen-address=:9122 <strong>test3:</strong> ./redis_exporter -redis-only-metrics -redis.addr 192.168.163.133:15389 -redis.password cc -web.listen-address=:9121 ./redis_exporter -redis-only-metrics -redis.addr 192.168.163.133:15489 -redis.password cc -web.listen-address=:9122 <strong>test4:</strong> ./redis_exporter -redis-only-metrics -redis.addr 192.168.163.134:15389 -redis.password cc -web.listen-address=:9121 ./redis_exporter -redis-only-metrics -redis.addr 192.168.163.134:15489 -redis.password cc -web.listen-address=:9122</span></code></pre> </div> <p><span style="font-size: 15px;">最后配置Grafana,效果:</span></p> <p><span style="font-size: 15px;"><img alt="" width="851" height="388" class="b-lazy" data-src="https://img2020.cnblogs.com/blog/163084/202004/163084-20200418221425783-1746840226.png" data-original="https://img2020.cnblogs.com/blog/163084/202004/163084-20200418221425783-1746840226.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /></span></p> <p><span style="font-size: 15px;"><strong>JSON Model:</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">{ "annotations": { "list": [ { "builtIn": 1, "datasource": "-- Grafana --", "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", "name": "Annotations &amp; Alerts", "type": "dashboard" } ] }, "editable": true, "gnetId": null, "graphTooltip": 0, "id": 13, "iteration": 1587219062782, "links": [], "panels": [ { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群个数", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 0, "y": 0 }, "id": 8, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(count(redis_up{cluster=~\"$cluster\"}&gt;0) by (cluster))", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 5, "y": 0 }, "id": 4, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_up{cluster=~\"$cluster\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群活跃节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 4, "x": 10, "y": 0 }, "id": 6, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_up{cluster=~\"$cluster\"}&gt;0)", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群活跃节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorPrefix": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "主节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 14, "y": 0 }, "id": 10, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{cluster=~\"$cluster\", role=\"master\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "主节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "从节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 19, "y": 0 }, "id": 12, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{cluster=~\"$cluster\", role=\"slave\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "从节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redus Cluster OPS指标", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 3 }, "hiddenSeries": false, "id": 16, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "ceil(sum(rate(redis_commands_processed_total{cluster=~\"$cluster\",instance=~\"$master\"}[$interval])) by (cluster))", "interval": "5s", "legendFormat": "{{cluster}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis Cluster OPS", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用大小值", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 3 }, "hiddenSeries": false, "id": 18, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_memory_used_bytes{instance=~\"$instance\"} ", "interval": "5s", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用大小", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "bytes", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis Server CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 11 }, "hiddenSeries": false, "id": 14, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "100* (rate(redis_cpu_sys_seconds_total{instance=~\"$instance\"}[$interval]) + rate(redis_cpu_user_seconds_total{instance=~\"$instance\"}[$interval]))", "interval": "5s", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 11 }, "hiddenSeries": false, "id": 20, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "(redis_memory_used_bytes{instance=~\"$instance\"} / redis_config_maxmemory{instance=~\"$instance\"}) * 100", "interval": "5s", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 19 }, "hiddenSeries": false, "id": 24, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "topk(3,clamp_max((avg by (hostname,mode) ((clamp_max(rate(node_cpu_seconds_total{hostname=~\"$host\",mode!=\"idle\"}[$interval]),1)) or (clamp_max(irate(node_cpu_seconds_total{hostname=~\"$host\",mode!=\"idle\"}[5m]),1)) ))*100,100))", "hide": true, "interval": "", "legendFormat": "{{mode}}", "refId": "A" }, { "expr": "1 - avg by (hostname)(rate(node_cpu_seconds_total{hostname=~\"$host\", mode=\"idle\"}[$interval]))", "interval": "", "legendFormat": "{{hostname}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percentunit", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统物理内存利用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 19 }, "hiddenSeries": false, "id": 30, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "(node_memory_Cached_bytes{hostname=~\"$host\"} + node_memory_Buffers_bytes{hostname=~\"$host\"} + node_memory_MemFree_bytes{hostname=~\"$host\"})/node_memory_MemTotal_bytes{hostname=~\"$host\"}", "hide": true, "interval": "", "legendFormat": "{{hostname}}", "refId": "A" }, { "expr": "((node_memory_MemTotal_bytes{hostname=~\"$host\"} - (node_memory_MemAvailable_bytes{hostname=~\"$host\"} or (node_memory_MemFree_bytes{hostname=~\"$host\"} + node_memory_Buffers_bytes{hostname=~\"$host\"} + node_memory_Cached_bytes{hostname=~\"$host\"})))*100 / node_memory_MemTotal_bytes{hostname=~\"$host\"})", "hide": false, "interval": "", "legendFormat": "{{hostname}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统物理内存利用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": true, "cacheTimeout": null, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统磁盘使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 27 }, "hiddenSeries": false, "id": 32, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": false, "linewidth": 1, "links": [], "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pluginVersion": "6.7.2", "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "topk(10,max((1 - node_filesystem_avail_bytes{hostname=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"} / node_filesystem_size_bytes{hostname=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"})*100) by (hostname) &gt; 0)", "interval": "", "legendFormat": "{{hostname}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统磁盘使用率", "tooltip": { "shared": false, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "series", "name": null, "show": true, "values": [ "current" ] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点以及集群的 KEYS 总数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 27 }, "hiddenSeries": false, "id": 22, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "sum(redis_db_keys{instance=~\"$master\"}) by (instance,db)", "interval": "", "legendFormat": "{{instance}}", "refId": "A" }, { "expr": "sum(redis_db_keys{instance=~\"$master\"}) by (cluster)", "interval": "", "legendFormat": "{{cluster}}_total", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "KEYS 总数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点活跃连接数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 35 }, "hiddenSeries": false, "id": 26, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_connected_clients{instance=~\"$instance\"}", "interval": "", "legendFormat": "{{hostip}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "活跃连接数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点内存碎片率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 35 }, "hiddenSeries": false, "id": 28, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_mem_fragmentation_ratio{instance=~\"$instance\"}", "interval": "", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "内存碎片率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点 的负载", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 43 }, "hiddenSeries": false, "id": 34, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "node_load1{hostname=~\"$host\"}", "interval": "", "legendFormat": "{{hostname}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "负载(Load)", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } } ], "refresh": "5s", "schemaVersion": 22, "style": "dark", "tags": [], "templating": { "list": [ { "auto": true, "auto_count": 100, "auto_min": "1s", "current": { "selected": true, "text": "5s", "value": "5s" }, "hide": 0, "label": "interval", "name": "interval", "options": [ { "selected": false, "text": "auto", "value": "$__auto_interval_interval" }, { "selected": true, "text": "5s", "value": "5s" }, { "selected": false, "text": "30s", "value": "30s" }, { "selected": false, "text": "1m", "value": "1m" }, { "selected": false, "text": "10m", "value": "10m" }, { "selected": false, "text": "30m", "value": "30m" }, { "selected": false, "text": "1h", "value": "1h" }, { "selected": false, "text": "6h", "value": "6h" }, { "selected": false, "text": "12h", "value": "12h" }, { "selected": false, "text": "1d", "value": "1d" } ], "query": "5s,30s,1m,10m,30m,1h,6h,12h,1d", "refresh": 2, "skipUrlSync": false, "type": "interval" }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_up,cluster)", "hide": 0, "includeAll": true, "index": -1, "label": "Cluster", "multi": true, "name": "cluster", "options": [], "query": "label_values(redis_up,cluster)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info,role)", "hide": 0, "includeAll": true, "index": -1, "label": "Role", "multi": true, "name": "role", "options": [], "query": "label_values(redis_instance_info,role)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info{cluster=~\"$cluster\",role=~\"$role\"},instance)", "hide": 2, "includeAll": true, "index": -1, "label": "instance", "multi": true, "name": "instance", "options": [], "query": "label_values(redis_instance_info{cluster=~\"$cluster\",role=~\"$role\"},instance)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info{cluster=~\"$cluster\",role=\"master\"},instance)", "hide": 2, "includeAll": true, "index": -1, "label": "master", "multi": true, "name": "master", "options": [], "query": "label_values(redis_instance_info{cluster=~\"$cluster\",role=\"master\"},instance)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_up{cluster=~\"$cluster\"}, hostip)", "hide": 2, "includeAll": true, "index": -1, "label": "host", "multi": true, "name": "host", "options": [], "query": "label_values(redis_up{cluster=~\"$cluster\"}, hostip)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false } ] }, "time": { "from": "now-5m", "to": "now" }, "timepicker": { "refresh_intervals": [ "5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d" ] }, "timezone": "", "title": "Redis_Cluster_V1.35", "uid": "5FfBHG3Zz", "variables": { "list": [] }, "version": 4 }</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">③:旧版本<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a> [<strong><a href="https://github.com/oliver006/redis_exporter/releases/tag/v0.34.1" rel="nofollow">v0.34.1</a></strong>]支持多实例不同密码,集群信息:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;">集群1:密码 cc M: 192.168.163.132:15389 192.168.163.133:15389 192.168.163.134:15389 S: 192.168.163.132:15489 192.168.163.133:15489 192.168.163.134:15489 集群2:密码abc M: 192.168.163.132:6379 192.168.163.133:6379 192.168.163.134:6379 S: 192.168.163.132:6381 192.168.163.133:6381 192.168.163.134:6381</span></code></pre> </div> <p><span style="font-size: 15px;"><strong>prometheus.yml:</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 rule_files: # - "first_rules.yml" # - "second_rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.163.132:9099'] - job_name: 'node_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/node/nodes-instances.json refresh_interval: 10s relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostip - action: labeldrop regex: __meta_filepath - job_name: 'redis_exporter' scrape_interval: 1s static_configs: - targets: ['192.168.163.132:9128']</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;"><a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a>直接用命令行启动:对应的参数有-redis.addr、-redis.alias、-redis.password:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;"># <strong>./redis_exporter -redis-only-metrics -redis.addr 192.168.163.132:15389,192.168.163.133:15389,192.168.163.134:15389,192.168.163.132:15489,192.168.163.133:15489,192.168.163.134:15489,192.168.163.132:6379,192.168.163.133:6379,192.168.163.134:6379,192.168.163.132:6381,192.168.163.133:6381,192.168.163.134:6381 -redis.alias test,test,test,test,test,test,test123,test123,test123,test123,test123,test123 -redis.password cc,cc,cc,cc,cc,cc,abc,abc,abc,abc,abc,abc -web.listen-address :9128</strong> INFO[0000] Redis Metrics Exporter v0.34.1 build date: 2019-05-09-15:42:21 sha1: 1c168d07eff09e64efb18600a35664d6666926e6 Go: go1.12.5 INFO[0000] Providing metrics at :9128/metrics INFO[0000] Connecting to redis hosts: []string{"192.168.163.132:15389", "192.168.163.133:15389", "192.168.163.134:15389", "192.168.163.132:15489", "192.168.163.133:15489", "192.168.163.134:15489", "192.168.163.132:6379", "192.168.163.133:6379", "192.168.163.134:6379", "192.168.163.132:6381", "192.168.163.133:6381", "192.168.163.134:6381"} INFO[0000] Using alias: []string{"test", "test", "test", "test", "test", "test", "test123", "test123", "test123", "test123", "test123", "test123"}</span></code></pre> </div> <p><span style="font-size: 15px;">注意:参数、密码和实例的他们是一一对应的。最后配置Grafana,效果:</span></p> <p><span style="font-size: 15px;"><img alt="" width="830" height="410" class="b-lazy" data-src="https://img2020.cnblogs.com/blog/163084/202004/163084-20200418223807226-733633803.png" data-original="https://img2020.cnblogs.com/blog/163084/202004/163084-20200418223807226-733633803.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /></span></p> <p><span style="font-size: 15px;"><strong>JSON Model:</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">{ "annotations": { "list": [ { "builtIn": 1, "datasource": "-- Grafana --", "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", "name": "Annotations &amp; Alerts", "type": "dashboard" } ] }, "editable": true, "gnetId": null, "graphTooltip": 0, "id": 14, "iteration": 1587221739977, "links": [], "panels": [ { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群个数", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 0, "y": 0 }, "id": 8, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(count(redis_up{alias=~\"$cluster\"}&gt;0) by (alias))", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 5, "y": 0 }, "id": 4, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{alias=~\"$cluster\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群活跃节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 4, "x": 10, "y": 0 }, "id": 6, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_up{alias=~\"$cluster\"}&gt;0)", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群活跃节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorPrefix": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "主节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 14, "y": 0 }, "id": 10, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{alias=~\"$cluster\", role=\"master\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "主节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "从节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 19, "y": 0 }, "id": 12, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{alias=~\"$cluster\", role=\"slave\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "从节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redus Cluster OPS指标", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 3 }, "hiddenSeries": false, "id": 16, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "ceil(sum(rate(redis_commands_processed_total{alias=~\"$cluster\",addr=~\"$master\"}[$interval])) by (alias))", "interval": "5s", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis Cluster OPS", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用大小值", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 3 }, "hiddenSeries": false, "id": 18, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_memory_used_bytes{addr=~\"$instance\"} ", "interval": "5s", "legendFormat": "{{addr}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用大小", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "bytes", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis Server CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 11 }, "hiddenSeries": false, "id": 14, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "100* (rate(redis_used_cpu_sys{addr=~\"$instance\"}[$interval]) + rate(redis_used_cpu_user{addr=~\"$instance\"}[$interval]))", "interval": "5s", "legendFormat": "{{addr}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 11 }, "hiddenSeries": false, "id": 20, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "(redis_memory_used_bytes{addr=~\"$instance\"} / redis_config_maxmemory{addr=~\"$instance\"}) * 100", "interval": "5s", "legendFormat": "{{addr}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 19 }, "hiddenSeries": false, "id": 24, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "topk(3,clamp_max((avg by (hostname,mode) ((clamp_max(rate(node_cpu_seconds_total{hostname=~\"$host\",mode!=\"idle\"}[$interval]),1)) or (clamp_max(irate(node_cpu_seconds_total{hostname=~\"$host\",mode!=\"idle\"}[5m]),1)) ))*100,100))", "hide": true, "interval": "", "legendFormat": "{{mode}}", "refId": "A" }, { "expr": "1 - avg by (hostip)(rate(node_cpu_seconds_total{hostip=~\"$host\", mode=\"idle\"}[$interval]))", "interval": "", "legendFormat": "{{hostip}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percentunit", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统物理内存利用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 19 }, "hiddenSeries": false, "id": 30, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "((node_memory_MemTotal_bytes{hostip=~\"$host\"} - (node_memory_MemAvailable_bytes{hostip=~\"$host\"} or (node_memory_MemFree_bytes{hostip=~\"$host\"} + node_memory_Buffers_bytes{hostip=~\"$host\"} + node_memory_Cached_bytes{hostip=~\"$host\"})))*100 / node_memory_MemTotal_bytes{hostip=~\"$host\"})", "hide": false, "interval": "", "legendFormat": "{{hostip}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统物理内存利用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": true, "cacheTimeout": null, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统磁盘使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 27 }, "hiddenSeries": false, "id": 32, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": false, "linewidth": 1, "links": [], "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pluginVersion": "6.7.2", "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "topk(10,max((1 - node_filesystem_avail_bytes{hostip=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"} / node_filesystem_size_bytes{hostip=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"})*100) by (hostip) &gt; 0)", "interval": "", "legendFormat": "{{hostip}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统磁盘使用率", "tooltip": { "shared": false, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "series", "name": null, "show": true, "values": [ "current" ] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点以及集群的 KEYS 总数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 27 }, "hiddenSeries": false, "id": 22, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "sum(redis_db_keys{addr=~\"$master\"}) by (addr,db)", "interval": "", "legendFormat": "{{addr}}", "refId": "A" }, { "expr": "sum(redis_db_keys{addr=~\"$master\"}) by (alias)", "interval": "", "legendFormat": "{{alias}}_total", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "KEYS 总数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点活跃连接数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 35 }, "hiddenSeries": false, "id": 26, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_connected_clients{addr=~\"$instance\"}", "interval": "", "legendFormat": "{{addr}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "活跃连接数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点内存碎片率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 35 }, "hiddenSeries": false, "id": 28, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_mem_fragmentation_ratio{addr=~\"$instance\"}", "interval": "", "legendFormat": "{{addr}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "内存碎片率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点 的负载", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 43 }, "hiddenSeries": false, "id": 34, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "node_load1{hostip=~\"$host\"}", "interval": "", "legendFormat": "{{hostip}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "负载(Load)", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } } ], "refresh": "5s", "schemaVersion": 22, "style": "dark", "tags": [], "templating": { "list": [ { "auto": true, "auto_count": 100, "auto_min": "1s", "current": { "selected": true, "text": "5s", "value": "5s" }, "hide": 0, "label": "interval", "name": "interval", "options": [ { "selected": false, "text": "auto", "value": "$__auto_interval_interval" }, { "selected": true, "text": "5s", "value": "5s" }, { "selected": false, "text": "30s", "value": "30s" }, { "selected": false, "text": "1m", "value": "1m" }, { "selected": false, "text": "10m", "value": "10m" }, { "selected": false, "text": "30m", "value": "30m" }, { "selected": false, "text": "1h", "value": "1h" }, { "selected": false, "text": "6h", "value": "6h" }, { "selected": false, "text": "12h", "value": "12h" }, { "selected": false, "text": "1d", "value": "1d" } ], "query": "5s,30s,1m,10m,30m,1h,6h,12h,1d", "refresh": 2, "skipUrlSync": false, "type": "interval" }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_up,alias)", "hide": 0, "includeAll": true, "index": -1, "label": "Cluster", "multi": true, "name": "cluster", "options": [], "query": "label_values(redis_up,alias)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info{alias=~\"$cluster\"},role)", "hide": 0, "includeAll": true, "index": -1, "label": "Role", "multi": true, "name": "role", "options": [], "query": "label_values(redis_instance_info{alias=~\"$cluster\"},role)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info{alias=~\"$cluster\",role=~\"$role\"},addr)", "hide": 2, "includeAll": true, "index": -1, "label": "instance", "multi": true, "name": "instance", "options": [], "query": "label_values(redis_instance_info{alias=~\"$cluster\",role=~\"$role\"},addr)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info{alias=~\"$cluster\",role=\"master\"},addr)", "hide": 2, "includeAll": true, "index": -1, "label": "master", "multi": true, "name": "master", "options": [], "query": "label_values(redis_instance_info{alias=~\"$cluster\",role=\"master\"},addr)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_up{alias=~\"$cluster\"}, addr)", "hide": 2, "includeAll": true, "index": -1, "label": "host", "multi": true, "name": "host", "options": [], "query": "label_values(redis_up{alias=~\"$cluster\"}, addr)", "refresh": 1, "regex": "(.*):.*", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false } ] }, "time": { "from": "now-5m", "to": "now" }, "timepicker": { "refresh_intervals": [ "5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d" ] }, "timezone": "", "title": "Redis_Cluster_V0.34", "uid": "5FfBHG3Zz1", "variables": { "list": [] }, "version": 1 }</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">3)导入<a href="https://grafana.com/grafana/dashboards/763" target="_blank" rel="nofollow">dashboard</a>(763)到Grafana,展示的监控图:</span></p> <p><img alt="" width="825" height="395" class="b-lazy" data-src="https://img2020.cnblogs.com/blog/163084/202004/163084-20200419000931566-140711010.png" data-original="https://img2020.cnblogs.com/blog/163084/202004/163084-20200419000931566-140711010.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /></p> <p><span style="font-size: 15px;">需要注意的是,监控面板上看到的是exporter的IP和端口,看不到Redis实例的端口,如果是一台机器部署一台Redis实例,则可以直接使用主机名。或则修改相应的labels来自定义需求。除了修改labels之外,还可以自定义数据源,如使用MySQL作为新数据源。</span></p> <p><span style="font-size: 15px;">5)MySQL做为数据源定制模板</span></p> <p><span style="font-size: 15px;">添加数据源:</span></p> <p><span style="font-size: 15px;"><img alt="" width="712" height="480" class="b-lazy" data-src="https://img2020.cnblogs.com/blog/163084/202004/163084-20200417231208844-527555052.png" data-original="https://img2020.cnblogs.com/blog/163084/202004/163084-20200417231208844-527555052.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /></span></p> <p><span style="font-size: 15px;">表结构:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span><span style="color: #000000;"> `cluster_info` ( `id` </span><span style="color: #0000ff;">bigint</span>(<span style="color: #800000; font-weight: bold;">20</span>) unsigned <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span> AUTO_INCREMENT COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">主键</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, `cluster_type` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">64</span>) <span style="color: #0000ff;">DEFAULT</span> <span style="color: #0000ff;">NULL</span> COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">集群类型</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, `cluster_name` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">64</span>) <span style="color: #0000ff;">DEFAULT</span> <span style="color: #0000ff;">NULL</span> COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">集群名</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, `hostname` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">64</span>) <span style="color: #0000ff;">DEFAULT</span> <span style="color: #0000ff;">NULL</span> COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">主机名</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, `host_ip` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">64</span>) <span style="color: #0000ff;">DEFAULT</span> <span style="color: #0000ff;">NULL</span> COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">IP</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, `port` </span><span style="color: #0000ff;">int</span>(<span style="color: #800000; font-weight: bold;">11</span>) <span style="color: #0000ff;">DEFAULT</span> <span style="color: #0000ff;">NULL</span> COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">服务端口</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, `role` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">64</span>) <span style="color: #0000ff;">DEFAULT</span> <span style="color: #0000ff;">NULL</span> COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">服务角色</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, `comments` </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">500</span>) <span style="color: #0000ff;">DEFAULT</span> <span style="color: #0000ff;">NULL</span> COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">集群描述</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, `create_time` </span><span style="color: #0000ff;">timestamp</span> <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span> <span style="color: #0000ff;">DEFAULT</span> <span style="color: #ff00ff;">CURRENT_TIMESTAMP</span> COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">创建时间</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, `update_time` </span><span style="color: #0000ff;">timestamp</span> <span style="color: #808080;">NOT</span> <span style="color: #0000ff;">NULL</span> <span style="color: #0000ff;">DEFAULT</span> <span style="color: #ff00ff;">CURRENT_TIMESTAMP</span> <span style="color: #0000ff;">ON</span> <span style="color: #0000ff;">UPDATE</span> <span style="color: #ff00ff;">CURRENT_TIMESTAMP</span> COMMENT <span style="color: #ff0000;">'</span><span style="color: #ff0000;">更新时间</span><span style="color: #ff0000;">'</span><span style="color: #000000;">, </span><span style="color: #0000ff;">PRIMARY</span> <span style="color: #0000ff;">KEY</span><span style="color: #000000;"> (`id`), </span><span style="color: #0000ff;">KEY</span><span style="color: #000000;"> `idx_create_time` (`create_time`), </span><span style="color: #0000ff;">KEY</span><span style="color: #000000;"> `idx_update_time` (`update_time`) ) ENGINE</span><span style="color: #808080;">=</span>InnoDB AUTO_INCREMENT<span style="color: #808080;">=</span><span style="color: #800000; font-weight: bold;">1</span> <span style="color: #0000ff;">DEFAULT</span> CHARSET<span style="color: #808080;">=</span>utf8 COMMENT<span style="color: #808080;">=</span><span style="color: #ff0000;">'</span><span style="color: #ff0000;">集群信息</span><span style="color: #ff0000;">'</span></code></pre> </div> <p><span style="font-size: 15px;">表记录:</span></p> <div class="cnblogs_code"> <pre><code>mysql<span style="color: #808080;">&gt;</span> <span style="color: #0000ff;">select</span> <span style="color: #808080;">*</span> <span style="color: #0000ff;">from</span><span style="color: #000000;"> cluster_info; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--+---------------+--------------+----------+-----------------+------+--------+--------------+---------------------+---------------------+</span> <span style="color: #808080;">|</span> id <span style="color: #808080;">|</span> cluster_type <span style="color: #808080;">|</span> cluster_name <span style="color: #808080;">|</span> hostname <span style="color: #808080;">|</span> host_ip <span style="color: #808080;">|</span> port <span style="color: #808080;">|</span> role <span style="color: #808080;">|</span> comments <span style="color: #808080;">|</span> create_time <span style="color: #808080;">|</span> update_time <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--+---------------+--------------+----------+-----------------+------+--------+--------------+---------------------+---------------------+</span> <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">1</span> <span style="color: #808080;">|</span> Redis_Cluster <span style="color: #808080;">|</span> testX <span style="color: #808080;">|</span> test2 <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">192.168</span>.<span style="color: #800000; font-weight: bold;">163.132</span> <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">6380</span> <span style="color: #808080;">|</span> master <span style="color: #808080;">|</span> 测试集群 <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">2020</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">04</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">08</span> <span style="color: #800000; font-weight: bold;">19</span>:<span style="color: #800000; font-weight: bold;">22</span>:<span style="color: #800000; font-weight: bold;">56</span> <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">2020</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">04</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">09</span> <span style="color: #800000; font-weight: bold;">11</span>:<span style="color: #800000; font-weight: bold;">19</span>:<span style="color: #800000; font-weight: bold;">13</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">2</span> <span style="color: #808080;">|</span> Redis_Cluster <span style="color: #808080;">|</span> testX <span style="color: #808080;">|</span> test3 <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">192.168</span>.<span style="color: #800000; font-weight: bold;">163.133</span> <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">6380</span> <span style="color: #808080;">|</span> master <span style="color: #808080;">|</span> 测试集群 <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">2020</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">04</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">08</span> <span style="color: #800000; font-weight: bold;">19</span>:<span style="color: #800000; font-weight: bold;">24</span>:<span style="color: #800000; font-weight: bold;">29</span> <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">2020</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">04</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">09</span> <span style="color: #800000; font-weight: bold;">11</span>:<span style="color: #800000; font-weight: bold;">19</span>:<span style="color: #800000; font-weight: bold;">16</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">3</span> <span style="color: #808080;">|</span> Redis_Cluster <span style="color: #808080;">|</span> testX <span style="color: #808080;">|</span> test4 <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">192.168</span>.<span style="color: #800000; font-weight: bold;">163.134</span> <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">6380</span> <span style="color: #808080;">|</span> master <span style="color: #808080;">|</span> 测试集群 <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">2020</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">04</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">08</span> <span style="color: #800000; font-weight: bold;">19</span>:<span style="color: #800000; font-weight: bold;">24</span>:<span style="color: #800000; font-weight: bold;">33</span> <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">2020</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">04</span><span style="color: #808080;">-</span><span style="color: #800000; font-weight: bold;">09</span> <span style="color: #800000; font-weight: bold;">11</span>:<span style="color: #800000; font-weight: bold;">19</span>:<span style="color: #800000; font-weight: bold;">16</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--+---------------+--------------+----------+-----------------+------+--------+--------------+---------------------+---------------------+</span></code></pre> </div> <p><strong><span style="font-size: 15px;">5.1 使用编写导出器的最佳实践:</span></strong></p> <p><span style="font-size: 15px;"><strong>prometheus.yml:</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">global: scrape_interval: 10s # </span><span style="color: #0000ff;">Set</span> the scrape interval <span style="color: #0000ff;">to</span> every <span style="color: #800000; font-weight: bold;">15</span> seconds. <span style="color: #0000ff;">Default</span> <span style="color: #0000ff;">is</span> every <span style="color: #800000; font-weight: bold;">1</span><span style="color: #000000;"> minute. evaluation_interval: 10s # Evaluate rules every </span><span style="color: #800000; font-weight: bold;">15</span> seconds. The <span style="color: #0000ff;">default</span> <span style="color: #0000ff;">is</span> every <span style="color: #800000; font-weight: bold;">1</span><span style="color: #000000;"> minute. alerting: alertmanagers: </span><span style="color: #808080;">-</span><span style="color: #000000;"> static_configs: </span><span style="color: #808080;">-</span><span style="color: #000000;"> targets: # </span><span style="color: #808080;">-</span> alertmanager:<span style="color: #800000; font-weight: bold;">9093</span><span style="color: #000000;"> rule_files: # </span><span style="color: #808080;">-</span><span style="color: #000000;"> "first_rules.yml" # </span><span style="color: #808080;">-</span><span style="color: #000000;"> "second_rules.yml" scrape_configs: </span><span style="color: #808080;">-</span> job_name: <span style="color: #ff0000;">'</span><span style="color: #ff0000;">prometheus</span><span style="color: #ff0000;">'</span><span style="color: #000000;"> static_configs: </span><span style="color: #808080;">-</span> targets: <span style="color: #ff0000;">[</span><span style="color: #ff0000;">'192.168.163.132:9099'</span><span style="color: #ff0000;">]</span> <span style="color: #808080;">-</span> job_name: <span style="color: #ff0000;">'</span><span style="color: #ff0000;">node_exporter</span><span style="color: #ff0000;">'</span><span style="color: #000000;"> scrape_interval: 1s file_sd_configs: </span><span style="color: #808080;">-</span><span style="color: #000000;"> files: </span><span style="color: #808080;">-</span> targets<span style="color: #808080;">/</span>node<span style="color: #808080;">/</span>node<span style="color: #808080;">*</span><span style="color: #000000;">.yml refresh_interval: 10s relabel_configs: </span><span style="color: #808080;">-</span> action: <span style="color: #ff00ff;">replace</span><span style="color: #000000;"> source_labels: </span><span style="color: #ff0000;">[</span><span style="color: #ff0000;">'__address__'</span><span style="color: #ff0000;">]</span><span style="color: #000000;"> regex: (.</span><span style="color: #808080;">*</span>):(.<span style="color: #808080;">*</span><span style="color: #000000;">) replacement: $</span><span style="color: #800000; font-weight: bold;">1</span><span style="color: #000000;"> target_label: hostname </span><span style="color: #808080;">-</span><span style="color: #000000;"> action: labeldrop regex: __meta_filepath </span><span style="color: #808080;">-</span> job_name: <span style="color: #ff0000;">'</span><span style="color: #ff0000;">redis_exporter</span><span style="color: #ff0000;">'</span><span style="color: #000000;"> scrape_interval: 1s file_sd_configs: </span><span style="color: #808080;">-</span><span style="color: #000000;"> files: </span><span style="color: #808080;">-</span> targets<span style="color: #808080;">/</span>redis<span style="color: #808080;">/</span>Xredis<span style="color: #808080;">*</span><span style="color: #000000;">.yml relabel_configs: </span><span style="color: #808080;">-</span> action: <span style="color: #ff00ff;">replace</span><span style="color: #000000;"> source_labels: </span><span style="color: #ff0000;">[</span><span style="color: #ff0000;">'__address__'</span><span style="color: #ff0000;">]</span><span style="color: #000000;"> regex: (.</span><span style="color: #808080;">*</span>):(.<span style="color: #808080;">*</span><span style="color: #000000;">) replacement: $</span><span style="color: #800000; font-weight: bold;">1</span><span style="color: #000000;"> target_label: hostip </span><span style="color: #808080;">-</span><span style="color: #000000;"> action: labeldrop regex: __meta_filepath</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;"><strong><span>targets/node/node*.yml</span></strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">root@test2:/etc/prometheus/targets/node# ls -lh node*.yml -rw-r--r-- 1 prometheus prometheus 103 4月 19 01:24 node_test2_exporter.yml -rw-r--r-- 1 prometheus prometheus 103 4月 18 16:18 node_test3_exporter.yml -rw-r--r-- 1 prometheus prometheus 103 4月 18 16:18 node_test4_exporter.yml root@test2:/etc/prometheus/targets/node# cat node_test2_exporter.yml [ { "targets": ["192.168.163.132:9199"], "labels": {"env":"test","instance":"test2"} } ] root@test2:/etc/prometheus/targets/node# cat node_test3_exporter.yml [ { "targets": ["192.168.163.133:9199"], "labels": {"env":"test","instance":"test3"} } ] root@test2:/etc/prometheus/targets/node# cat node_test4_exporter.yml [ { "targets": ["192.168.163.134:9199"], "labels": {"env":"test","instance":"test4"} } ]</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;"><strong><span>targets/redis/Xredis*.yml</span></strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">root@test2:/etc/prometheus/targets/redis# ls -lh Xredis*.yml -rw-r--r-- 1 root root 118 4月 19 00:34 Xredis_test2_6380_exporter.yml -rw-r--r-- 1 root root 118 4月 19 00:36 Xredis_test3_6380_exporter.yml -rw-r--r-- 1 root root 118 4月 19 00:35 Xredis_test4_6380_exporter.yml root@test2:/etc/prometheus/targets/redis# cat Xredis_test2_6380_exporter.yml [ { "targets": ["192.168.163.132:9123"], "labels": {"env":"test","cluster":"testX","alias":"test2"} } ] root@test2:/etc/prometheus/targets/redis# cat Xredis_test3_6380_exporter.yml [ { "targets": ["192.168.163.133:9123"], "labels": {"env":"test","cluster":"testX","alias":"test3"} } ] root@test2:/etc/prometheus/targets/redis# cat Xredis_test4_6380_exporter.yml [ { "targets": ["192.168.163.134:9123"], "labels": {"env":"test","cluster":"testX","alias":"test4"} } ]</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">配置完成之后,可以直接到各个监控主机上启动<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a>:</span></p> <div class="cnblogs_code"> <pre><code><span style="color: #000000;"><strong>test2</strong>: ./redis_exporter -redis-only-metrics -redis.addr 192.168.163.132:6380 -redis.password cba -web.listen-address=:9123 <strong>test3</strong>: ./redis_exporter -redis-only-metrics -redis.addr 192.168.163.133:6380 -redis.password cba -web.listen-address=:9123 <strong>test4</strong>: ./redis_exporter -redis-only-metrics -redis.addr 192.168.163.134:6380 -redis.password cba -web.listen-address=:9123</span></code></pre> </div> <p><span style="font-size: 15px;">最后配置Grafana,效果:</span></p> <p><span style="font-size: 15px;"><img alt="" width="870" height="388" class="b-lazy" data-src="https://img2020.cnblogs.com/blog/163084/202004/163084-20200419013757420-1239998565.png" data-original="https://img2020.cnblogs.com/blog/163084/202004/163084-20200419013757420-1239998565.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /></span></p> <p><span style="font-size: 15px;"><strong>JSON Model:</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">{ "annotations": { "list": [ { "builtIn": 1, "datasource": "-- Grafana --", "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", "name": "Annotations &amp; Alerts", "type": "dashboard" } ] }, "editable": true, "gnetId": null, "graphTooltip": 0, "id": 13, "iteration": 1587231669738, "links": [], "panels": [ { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群个数", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 0, "y": 0 }, "id": 8, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(count(redis_up{cluster=~\"$cluster\"}) by (cluster))", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 5, "y": 0 }, "id": 4, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_up{cluster=~\"$cluster\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群活跃节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 4, "x": 10, "y": 0 }, "id": 6, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_up{cluster=~\"$cluster\"}&gt;0)", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群活跃节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorPrefix": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "主节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 14, "y": 0 }, "id": 10, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{cluster=~\"$cluster\", role=\"master\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "主节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "从节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 19, "y": 0 }, "id": 12, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{cluster=~\"$cluster\", role=\"slave\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "从节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redus Cluster OPS指标", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 3 }, "hiddenSeries": false, "id": 16, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "ceil(sum(rate(redis_commands_processed_total{cluster=~\"$cluster\",alias=~\"$master\"}[$interval])) by (cluster))", "interval": "5s", "legendFormat": "{{cluster}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis Cluster OPS", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用大小值", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 3 }, "hiddenSeries": false, "id": 18, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_memory_used_bytes{alias=~\"$host\"} ", "interval": "5s", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用大小", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "bytes", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis Server CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 11 }, "hiddenSeries": false, "id": 14, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "100* (rate(redis_cpu_sys_seconds_total{alias=~\"$host\"}[$interval]) + rate(redis_cpu_user_seconds_total{alias=~\"$host\"}[$interval]))", "interval": "5s", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 11 }, "hiddenSeries": false, "id": 20, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "(redis_memory_used_bytes{alias=~\"$host\"} / redis_config_maxmemory{alias=~\"$host\"}) * 100", "interval": "5s", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 19 }, "hiddenSeries": false, "id": 24, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "1 - avg by (instance)(rate(node_cpu_seconds_total{instance=~\"$host\", mode=\"idle\"}[$interval]))", "interval": "", "legendFormat": "{{instance}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percentunit", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统物理内存利用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 19 }, "hiddenSeries": false, "id": 30, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "((node_memory_MemTotal_bytes{instance=~\"$host\"} - (node_memory_MemAvailable_bytes{instance=~\"$host\"} or (node_memory_MemFree_bytes{instance=~\"$host\"} + node_memory_Buffers_bytes{instance=~\"$host\"} + node_memory_Cached_bytes{instance=~\"$host\"})))*100 / node_memory_MemTotal_bytes{instance=~\"$host\"})", "hide": false, "interval": "", "legendFormat": "{{instance}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统物理内存利用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": true, "cacheTimeout": null, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统磁盘使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 27 }, "hiddenSeries": false, "id": 32, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": false, "linewidth": 1, "links": [], "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pluginVersion": "6.7.2", "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "topk(10,max((1 - node_filesystem_avail_bytes{instance=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"} / node_filesystem_size_bytes{instance=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"})*100) by (instance) &gt; 0)", "interval": "", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统磁盘使用率", "tooltip": { "shared": false, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "series", "name": null, "show": true, "values": [ "current" ] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点以及集群的 KEYS 总数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 27 }, "hiddenSeries": false, "id": 22, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "sum(redis_db_keys{alias=~\"$master\"}) by (alias,db)", "interval": "", "legendFormat": "{{alias}}", "refId": "A" }, { "expr": "sum(redis_db_keys{alias=~\"$master\"}) by (cluster)", "hide": false, "interval": "", "legendFormat": "{{cluster}}_total", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "KEYS 总数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "集群的活跃连接数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 35 }, "hiddenSeries": false, "id": 26, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "sum(redis_connected_clients{alias=~\"$master\"}) by (cluster)", "interval": "", "legendFormat": "{{cluster}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "活跃连接数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点内存碎片率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 35 }, "hiddenSeries": false, "id": 28, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_mem_fragmentation_ratio{alias=~\"$host\"}", "interval": "", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "内存碎片率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点 的负载", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 43 }, "hiddenSeries": false, "id": 34, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "node_load1{instance=~\"$host\"}", "interval": "", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "负载(Load)", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } } ], "refresh": "5s", "schemaVersion": 22, "style": "dark", "tags": [], "templating": { "list": [ { "auto": true, "auto_count": 100, "auto_min": "1s", "current": { "selected": false, "text": "5s", "value": "5s" }, "hide": 0, "label": "interval", "name": "interval", "options": [ { "selected": false, "text": "auto", "value": "$__auto_interval_interval" }, { "selected": true, "text": "5s", "value": "5s" }, { "selected": false, "text": "30s", "value": "30s" }, { "selected": false, "text": "1m", "value": "1m" }, { "selected": false, "text": "10m", "value": "10m" }, { "selected": false, "text": "30m", "value": "30m" }, { "selected": false, "text": "1h", "value": "1h" }, { "selected": false, "text": "6h", "value": "6h" }, { "selected": false, "text": "12h", "value": "12h" }, { "selected": false, "text": "1d", "value": "1d" } ], "query": "5s,30s,1m,10m,30m,1h,6h,12h,1d", "refresh": 2, "skipUrlSync": false, "type": "interval" }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select cluster_name from cluster_info where cluster_type = \"Redis_Cluster\"", "hide": 0, "includeAll": true, "index": -1, "label": "Cluster", "multi": true, "name": "cluster", "options": [], "query": "select cluster_name from cluster_info where cluster_type = \"Redis_Cluster\"", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select role from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster)", "hide": 0, "includeAll": true, "index": -1, "label": "Role", "multi": true, "name": "role", "options": [], "query": "select role from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster)", "hide": 2, "includeAll": true, "index": -1, "label": "host", "multi": true, "name": "host", "options": [], "query": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role=\"master\"", "hide": 2, "includeAll": true, "index": -1, "label": "master", "multi": true, "name": "master", "options": [], "query": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role=\"master\"", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role=\"slave\"", "hide": 2, "includeAll": true, "index": -1, "label": "slave", "multi": true, "name": "slave", "options": [], "query": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role=\"slave\"", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "Prometheus", "definition": "label_values(redis_instance_info{cluster=~\"$cluster\",role=~\"$role\"},instance)", "hide": 2, "includeAll": true, "index": -1, "label": "instance", "multi": true, "name": "instance", "options": [], "query": "label_values(redis_instance_info{cluster=~\"$cluster\",role=~\"$role\"},instance)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false } ] }, "time": { "from": "now-5m", "to": "now" }, "timepicker": { "refresh_intervals": [ "5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d" ] }, "timezone": "", "title": "Redis_Cluster_V1.35", "uid": "5FfBHG3Zz", "variables": { "list": [] }, "version": 9 }</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;"><strong>5.2 使用老版本的exporter:</strong></span></p> <p><span style="font-size: 15px;"><strong>prometheus.yml</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 rule_files: # - "first_rules.yml" # - "second_rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.163.132:9099'] - job_name: 'node_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/node/node*.yml refresh_interval: 10s relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostname - action: labeldrop regex: __meta_filepath - job_name: 'redis_exporter' scrape_interval: 1s static_configs: - targets: ['192.168.163.132:9128']</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">配置完成之后,可以直接到监控主机上启动<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a>:</span></p> <div class="cnblogs_code"> <pre><code>./redis_exporter -redis-only-metrics -redis.addr 192.168.163.132:6380,192.168.163.133:6380,192.168.163.134:6380 -redis.alias test2,test3,test4 -redis.password cba,cba,cba -web.listen-address :9128</code></pre> </div> <p><span style="font-size: 15px;">最后配置Grafana,效果:</span></p> <p><img alt="" width="855" height="418" class="b-lazy" data-src="https://img2020.cnblogs.com/blog/163084/202004/163084-20200419023159779-2106595841.png" data-original="https://img2020.cnblogs.com/blog/163084/202004/163084-20200419023159779-2106595841.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /></p> <p><span style="font-size: 15px;"><strong>JSON Model:</strong></span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">{ "annotations": { "list": [ { "builtIn": 1, "datasource": "-- Grafana --", "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", "name": "Annotations &amp; Alerts", "type": "dashboard" } ] }, "editable": true, "gnetId": null, "graphTooltip": 0, "id": 13, "iteration": 1587234344797, "links": [], "panels": [ { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": "MySQL", "description": "集群个数", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 0, "y": 0 }, "id": 8, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "format": "table", "group": [], "metricColumn": "none", "rawQuery": true, "rawSql": "select count(distinct cluster_name) from cluster_info where cluster_type = 'Redis_Cluster'", "refId": "A", "select": [ [ { "params": [ "id" ], "type": "column" } ] ], "table": "cluster_info", "timeColumn": "create_time", "timeColumnType": "timestamp", "where": [ { "name": "$__timeFilter", "params": [], "type": "macro" } ] } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 5, "y": 0 }, "id": 4, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_up{alias=~\"$all_host\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "集群活跃节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 4, "x": 10, "y": 0 }, "id": 6, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_up{alias=~\"$all_host\"}&gt;0)", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "集群活跃节点数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorPrefix": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "主节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 14, "y": 0 }, "id": 10, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{alias=~\"$master\", role=\"master\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "主节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "cacheTimeout": null, "colorBackground": false, "colorValue": true, "colors": [ "#299c46", "rgba(237, 129, 40, 0.89)", "#d44a3a" ], "datasource": null, "description": "从节点数量", "format": "none", "gauge": { "maxValue": 100, "minValue": 0, "show": false, "thresholdLabels": false, "thresholdMarkers": true }, "gridPos": { "h": 3, "w": 5, "x": 19, "y": 0 }, "id": 12, "interval": null, "links": [], "mappingType": 1, "mappingTypes": [ { "name": "value to text", "value": 1 }, { "name": "range to text", "value": 2 } ], "maxDataPoints": 100, "nullPointMode": "connected", "nullText": null, "postfix": "", "postfixFontSize": "50%", "prefix": "", "prefixFontSize": "50%", "rangeMaps": [ { "from": "null", "text": "N/A", "to": "null" } ], "sparkline": { "fillColor": "rgba(31, 118, 189, 0.18)", "full": false, "lineColor": "rgb(31, 120, 193)", "show": false, "ymax": null, "ymin": null }, "tableColumn": "", "targets": [ { "expr": "count(redis_instance_info{alias=~\"$slave\", role=\"slave\"})", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": "", "timeFrom": null, "timeShift": null, "title": "从节点个数", "type": "singlestat", "valueFontSize": "100%", "valueMaps": [ { "op": "=", "text": "N/A", "value": "null" } ], "valueName": "current" }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redus Cluster OPS指标", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 3 }, "hiddenSeries": false, "id": 16, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "ceil(sum(rate(redis_commands_processed_total{alias=~\"$master\"}[$interval])))", "interval": "5s", "legendFormat": "", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis Cluster OPS", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用大小值", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 3 }, "hiddenSeries": false, "id": 18, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_memory_used_bytes{alias=~\"$host\"} ", "interval": "5s", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用大小", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "bytes", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis Server CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 11 }, "hiddenSeries": false, "id": 14, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "100* (rate(redis_used_cpu_sys{alias=~\"$host\"}[$interval]) + rate(redis_used_cpu_user{alias=~\"$host\"}[$interval]))", "interval": "5s", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "Redis 内存使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 11 }, "hiddenSeries": false, "id": 20, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "(redis_memory_used_bytes{alias=~\"$host\"} / redis_config_maxmemory{alias=~\"$host\"}) * 100", "interval": "5s", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "Redis 内存使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统CPU使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 19 }, "hiddenSeries": false, "id": 24, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "1 - avg by (instance)(rate(node_cpu_seconds_total{instance=~\"$host\", mode=\"idle\"}[$interval]))", "interval": "", "legendFormat": "{{instance}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统CPU使用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percentunit", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统物理内存利用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 19 }, "hiddenSeries": false, "id": 30, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "((node_memory_MemTotal_bytes{instance=~\"$host\"} - (node_memory_MemAvailable_bytes{instance=~\"$host\"} or (node_memory_MemFree_bytes{instance=~\"$host\"} + node_memory_Buffers_bytes{instance=~\"$host\"} + node_memory_Cached_bytes{instance=~\"$host\"})))*100 / node_memory_MemTotal_bytes{instance=~\"$host\"})", "hide": false, "interval": "", "legendFormat": "{{instance}}", "refId": "B" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统物理内存利用率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": true, "cacheTimeout": null, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点系统磁盘使用率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 27 }, "hiddenSeries": false, "id": 32, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": false, "linewidth": 1, "links": [], "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pluginVersion": "6.7.2", "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "topk(10,max((1 - node_filesystem_avail_bytes{instance=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"} / node_filesystem_size_bytes{instance=~\"$host\", fstype!~\"rootfs|selinuxfs|autofs|rpc_pipefs|tmpfs\"})*100) by (instance) &gt; 0)", "interval": "", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "系统磁盘使用率", "tooltip": { "shared": false, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "series", "name": null, "show": true, "values": [ "current" ] }, "yaxes": [ { "format": "percent", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点以及集群的 KEYS 总数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 27 }, "hiddenSeries": false, "id": 22, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "sum(redis_db_keys{alias=~\"$master\"}) by (alias,db)", "interval": "", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "KEYS 总数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "集群的活跃连接数", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 35 }, "hiddenSeries": false, "id": 26, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "sum(redis_connected_clients{alias=~\"$host\"}) by (cluster)", "interval": "", "legendFormat": "{{cluster}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "活跃连接数", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点内存碎片率", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 12, "y": 35 }, "hiddenSeries": false, "id": 28, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "redis_mem_fragmentation_ratio{alias=~\"$host\"}", "interval": "", "legendFormat": "{{alias}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "内存碎片率", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "各个节点 的负载", "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 43 }, "hiddenSeries": false, "id": 34, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "options": { "dataLinks": [] }, "percentage": false, "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "node_load1{instance=~\"$host\"}", "interval": "", "legendFormat": "{{instance}}", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "负载(Load)", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } } ], "refresh": "5s", "schemaVersion": 22, "style": "dark", "tags": [], "templating": { "list": [ { "auto": true, "auto_count": 100, "auto_min": "1s", "current": { "selected": false, "text": "5s", "value": "5s" }, "hide": 0, "label": "interval", "name": "interval", "options": [ { "selected": false, "text": "auto", "value": "$__auto_interval_interval" }, { "selected": true, "text": "5s", "value": "5s" }, { "selected": false, "text": "30s", "value": "30s" }, { "selected": false, "text": "1m", "value": "1m" }, { "selected": false, "text": "10m", "value": "10m" }, { "selected": false, "text": "30m", "value": "30m" }, { "selected": false, "text": "1h", "value": "1h" }, { "selected": false, "text": "6h", "value": "6h" }, { "selected": false, "text": "12h", "value": "12h" }, { "selected": false, "text": "1d", "value": "1d" } ], "query": "5s,30s,1m,10m,30m,1h,6h,12h,1d", "refresh": 2, "skipUrlSync": false, "type": "interval" }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select cluster_name from cluster_info where cluster_type = \"Redis_Cluster\"", "hide": 0, "includeAll": true, "index": -1, "label": "Cluster", "multi": true, "name": "cluster", "options": [], "query": "select cluster_name from cluster_info where cluster_type = \"Redis_Cluster\"", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select role from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster)", "hide": 0, "includeAll": true, "index": -1, "label": "Role", "multi": true, "name": "role", "options": [], "query": "select role from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role in ($role)", "hide": 2, "includeAll": true, "index": -1, "label": "host", "multi": true, "name": "host", "options": [], "query": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role in ($role)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role=\"master\"", "hide": 2, "includeAll": true, "index": -1, "label": "master", "multi": true, "name": "master", "options": [], "query": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role=\"master\"", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role=\"slave\"", "hide": 2, "includeAll": true, "index": -1, "label": "slave", "multi": true, "name": "slave", "options": [], "query": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster) and role=\"slave\"", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false }, { "allValue": null, "current": { "text": "All", "value": [ "$__all" ] }, "datasource": "MySQL", "definition": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster)", "hide": 2, "includeAll": true, "index": -1, "label": "all_host", "multi": true, "name": "all_host", "options": [], "query": "select hostname from cluster_info where cluster_type = \"Redis_Cluster\" and cluster_name in ($cluster)", "refresh": 1, "regex": "", "skipUrlSync": false, "sort": 0, "tagValuesQuery": "", "tags": [], "tagsQuery": "", "type": "query", "useTags": false } ] }, "time": { "from": "now-5m", "to": "now" }, "timepicker": { "refresh_intervals": [ "5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d" ] }, "timezone": "", "title": "Redis_Cluster_V1.35", "uid": "5FfBHG3Zz", "variables": { "list": [] }, "version": 14 }</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">从上面的图中看到,其中OPS的Legend展示的是表达式,按照老版本的的redis_exporter,不能自定义label,虽然监控不同密码多实例比较方便,但作图的时候不好看。目前想到二种方法:</span></p> <p><span style="font-size: 15px;">1. 使用推荐的一个实例一个exporter。</span></p> <p><span style="font-size: 15px;">2. 在老版本redis_exporter基础上,对指标进行重新定义和打label,此时需要自己编写一个exporter来对指标进行重写。</span></p> <p><span style="font-size: 15px;">如:老版本的redis_exporter部署在137这台机器上,并且端口为9128:使用python的<a href="https://github.com/prometheus/client_python/blob/master/README.md" target="_blank" rel="nofollow">prometheus_client</a>模块编写一个服务(5000)来抓取数据进行label修改:</span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #008000;">#</span><span style="color: #008000;">!/bin/env python</span><span style="color: #008000;"> #</span><span style="color: #008000;"> -*- encoding: utf-8 -*-</span> <span style="color: #0000ff;">import</span><span style="color: #000000;"> requests </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> re </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> json </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> MySQLdb </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> prometheus_client </span><span style="color: #0000ff;">from</span> prometheus_client <span style="color: #0000ff;">import</span><span style="color: #000000;"> Gauge,Counter </span><span style="color: #0000ff;">from</span> flask <span style="color: #0000ff;">import</span><span style="color: #000000;"> Response, Flask app </span>= Flask(<span style="color: #800080;">__name__</span><span style="color: #000000;">) </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> redis_cluster_metadata(): conn </span>= MySQLdb.connect(host=<span style="color: #800000;">"</span><span style="color: #800000;">192.168.163.133</span><span style="color: #800000;">"</span>, port=3306, user=<span style="color: #800000;">"</span><span style="color: #800000;">dba</span><span style="color: #800000;">"</span>, passwd=<span style="color: #800000;">"</span><span style="color: #800000;">dba</span><span style="color: #800000;">"</span>, db=<span style="color: #800000;">'</span><span style="color: #800000;">test</span><span style="color: #800000;">'</span>, charset=<span style="color: #800000;">'</span><span style="color: #800000;">utf8</span><span style="color: #800000;">'</span><span style="color: #000000;">) query </span>= <span style="color: #800000;">'''</span><span style="color: #800000;"> SELECT concat( host_ip, ':', PORT ) ip, cluster_name FROM cluster_info WHERE cluster_type = 'Redis_Cluster' </span><span style="color: #800000;">'''</span><span style="color: #000000;"> cursor </span>=<span style="color: #000000;"> conn.cursor() cursor.execute(query) conn.commit() result </span>=<span style="color: #000000;"> cursor.fetchall() cursor.close() </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> gauge_init(pair): result </span>=<span style="color: #000000;"> {} </span><span style="color: #0000ff;">for</span> k, v <span style="color: #0000ff;">in</span><span style="color: #000000;"> pair.items(): name </span>=<span style="color: #000000;"> v documentation </span>=<span style="color: #000000;"> v label </span>= (<span style="color: #800000;">"</span><span style="color: #800000;">alias</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">addr</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">cluster</span><span style="color: #800000;">"</span><span style="color: #000000;">) gauge </span>=<span style="color: #000000;"> Gauge(name, documentation, label) result[v] </span>=<span style="color: #000000;"> gauge </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> redis_cluster_metric(pair): url </span>= <span style="color: #800000;">"</span><span style="color: #800000;">http://192.168.163.137:9128/metrics</span><span style="color: #800000;">"</span><span style="color: #000000;"> pat </span>= re.compile(r<span style="color: #800000;">'</span><span style="color: #800000;">addr="(.*)",alias="(.*)"</span><span style="color: #800000;">'</span><span style="color: #000000;">) request_data </span>= requests.get(url, timeout=3<span style="color: #000000;">) metrics_info </span>=<span style="color: #000000;"> request_data.text metrics_dict </span>=<span style="color: #000000;"> {} </span><span style="color: #0000ff;">for</span> metrics <span style="color: #0000ff;">in</span> metrics_info.split(<span style="color: #800000;">'</span><span style="color: #800000;">\n</span><span style="color: #800000;">'</span><span style="color: #000000;">): </span><span style="color: #0000ff;">for</span> metric <span style="color: #0000ff;">in</span><span style="color: #000000;"> pair: </span><span style="color: #0000ff;">if</span> metrics.find(metric) ==<span style="color: #000000;"> 0: value </span>= metrics.split(<span style="color: #800000;">'</span> <span style="color: #800000;">'</span>)[1<span style="color: #000000;">] pat_info </span>=<span style="color: #000000;"> pat.search(metrics) ip </span>= pat_info.group(1<span style="color: #000000;">) alias </span>= pat_info.group(2<span style="color: #000000;">) metrics_dict.setdefault(ip, {}) metrics_dict[ip][</span><span style="color: #800000;">'</span><span style="color: #800000;">alias</span><span style="color: #800000;">'</span>] =<span style="color: #000000;"> alias metrics_dict[ip][metrics_pair[metric]] </span>=<span style="color: #000000;"> value </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> metrics_dict @app.route(</span><span style="color: #800000;">"</span><span style="color: #800000;">/metrics</span><span style="color: #800000;">"</span><span style="color: #000000;">) </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> prometheus_for_redis(): redis_cluster_info </span>=<span style="color: #000000;"> redis_cluster_metadata() metadata_dict </span>=<span style="color: #000000;"> {} </span><span style="color: #0000ff;">for</span> redis_cluster_item <span style="color: #0000ff;">in</span><span style="color: #000000;"> redis_cluster_info: ip,clustername </span>=<span style="color: #000000;"> redis_cluster_item metadata_dict[ip] </span>=<span style="color: #000000;"> clustername metrics_dict </span>=<span style="color: #000000;"> redis_cluster_metric(metrics_pair) </span><span style="color: #0000ff;">for</span> k,v <span style="color: #0000ff;">in</span><span style="color: #000000;"> metrics_dict.items(): </span><span style="color: #0000ff;">for</span> _k,_v <span style="color: #0000ff;">in</span><span style="color: #000000;"> metadata_dict.items(): </span><span style="color: #0000ff;">if</span> k ==<span style="color: #000000;"> _k: metrics_dict[k][</span><span style="color: #800000;">'</span><span style="color: #800000;">cluster</span><span style="color: #800000;">'</span>] =<span style="color: #000000;"> _v </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> metadata_dict </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> metrics_dict result </span>= <span style="color: #800000;">''</span> <span style="color: #0000ff;">for</span> metric_name, gauge <span style="color: #0000ff;">in</span><span style="color: #000000;"> gauge_dict.items(): </span><span style="color: #0000ff;">for</span> k, v <span style="color: #0000ff;">in</span><span style="color: #000000;"> metrics_dict.items(): </span><span style="color: #0000ff;">if</span> k <span style="color: #0000ff;">in</span><span style="color: #000000;"> metadata_dict.keys(): gauge.labels(alias</span>=v[<span style="color: #800000;">'</span><span style="color: #800000;">alias</span><span style="color: #800000;">'</span>], addr=k, cluster=v[<span style="color: #800000;">'</span><span style="color: #800000;">cluster</span><span style="color: #800000;">'</span><span style="color: #000000;">]).set(v[metric_name]) result </span>= result +<span style="color: #000000;"> prometheus_client.generate_latest(gauge).decode() </span><span style="color: #0000ff;">return</span> Response(result, mimetype=<span style="color: #800000;">"</span><span style="color: #800000;">text/plain</span><span style="color: #800000;">"</span><span style="color: #000000;">) </span><span style="color: #0000ff;">if</span> <span style="color: #800080;">__name__</span> == <span style="color: #800000;">"</span><span style="color: #800000;">__main__</span><span style="color: #800000;">"</span><span style="color: #000000;">: metrics_pair </span>= {<span style="color: #800000;">'</span><span style="color: #800000;">redis_commands_processed_total</span><span style="color: #800000;">'</span>: <span style="color: #800000;">'</span><span style="color: #800000;">cluster_redis_commands_processed_total</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">redis_cluster_size</span><span style="color: #800000;">'</span>:<span style="color: #800000;">'</span><span style="color: #800000;">cluster_redis_cluster_size</span><span style="color: #800000;">'</span><span style="color: #000000;">} gauge_dict </span>=<span style="color: #000000;"> gauge_init(metrics_pair) app.run(host</span>=<span style="color: #800000;">"</span><span style="color: #800000;">0.0.0.0</span><span style="color: #800000;">"</span>)</code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">大致的思想是:对原始的redis_exporter的指标进行过滤,拿出需要的指标进行label指定,最后作为一个导出器服务启动(5000),并配置到prometheus.yml里:</span></p> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code><span style="color: #000000;">global: scrape_interval: 5s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 5s # Evaluate rules every 15 seconds. The default is every 1 minute. alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 rule_files: # - "first_rules.yml" # - "second_rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['192.168.163.137:9099'] - job_name: 'node_exporter' scrape_interval: 1s file_sd_configs: - files: - targets/node/node*.yml refresh_interval: 10s relabel_configs: - action: replace source_labels: ['__address__'] regex: (.*):(.*) replacement: $1 target_label: hostname - action: labeldrop regex: __meta_filepath - job_name: 'redis_exporter' scrape_interval: 1s static_configs: - targets: ['192.168.163.137:9128'] - job_name: 'redisx_exporter' metrics_path: /redisx_cluster/metrics scrape_interval: 1s static_configs: - targets: ['192.168.163.137:5000']</span></code></pre> </div> <span>View Code</span> </div> <div class="cnblogs_code"> <img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><img alt="" class="b-lazy" data-src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" data-original="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><div class="cnblogs_code_hide"> <pre><code>/Users/jinyizhou [<span style="color: #800080;">18</span>:<span style="color: #800080;">51</span>:<span style="color: #800080;">25</span>] ~$ curl http:<span style="color: #008000;">//</span><span style="color: #008000;">0.0.0.0:5000/metrics</span> <span style="color: #000000;"># HELP cluster_redis_commands_processed_total cluster_redis_commands_processed_total # TYPE cluster_redis_commands_processed_total gauge cluster_redis_commands_processed_total{addr</span>=<span style="color: #800000;">"</span><span style="color: #800000;">192.168.163.137:6380</span><span style="color: #800000;">"</span>,alias=<span style="color: #800000;">"</span><span style="color: #800000;">test2</span><span style="color: #800000;">"</span>,cluster=<span style="color: #800000;">"</span><span style="color: #800000;">testX</span><span style="color: #800000;">"</span>} <span style="color: #800080;">1796512.0</span><span style="color: #000000;"> cluster_redis_commands_processed_total{addr</span>=<span style="color: #800000;">"</span><span style="color: #800000;">192.168.163.133:6380</span><span style="color: #800000;">"</span>,alias=<span style="color: #800000;">"</span><span style="color: #800000;">test3</span><span style="color: #800000;">"</span>,cluster=<span style="color: #800000;">"</span><span style="color: #800000;">testX</span><span style="color: #800000;">"</span>} <span style="color: #800080;">1592838.0</span><span style="color: #000000;"> cluster_redis_commands_processed_total{addr</span>=<span style="color: #800000;">"</span><span style="color: #800000;">192.168.163.134:6380</span><span style="color: #800000;">"</span>,alias=<span style="color: #800000;">"</span><span style="color: #800000;">test4</span><span style="color: #800000;">"</span>,cluster=<span style="color: #800000;">"</span><span style="color: #800000;">testX</span><span style="color: #800000;">"</span>} <span style="color: #800080;">1592053.0</span></code></pre> </div> <span>View Code</span> </div> <p><span style="font-size: 15px;">最后OPS的Legend显示为:<br /><img alt="" width="501" height="229" class="b-lazy" data-src="https://img2020.cnblogs.com/blog/163084/202004/163084-20200423201741861-888290651.png" data-original="https://img2020.cnblogs.com/blog/163084/202004/163084-20200423201741861-888290651.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><br /></span></p> <span id="OSC_h3_5"></span> <h3>小结:</h3> <p><span style="font-size: 15px;">上面的给出的这些参考指标以及监控项可以按照需要自行进行调整,如果需要做类似redis_exporter的导出器,可以连接Redis各个节点进行采样。具体的一些方法可以参考<a href="https://github.com/prometheus/client_python" target="_blank" rel="nofollow">client_python</a>。</span></p> <span id="OSC_h2_6"></span> <h2>总结:</h2> <p><span style="font-size: 15px;">本文大致介绍了Prometheus 监控Redis(cluster)的方法,后续会不定期进行对监控的完善。<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a>在v1.0之后做了比较大的<a href="https://github.com/oliver006/redis_exporter/pull/256" target="_blank" rel="nofollow">调整</a>,特别是对多实例不同密码这一块的改变,推荐还是最新版本的<a href="https://github.com/oliver006/redis_exporter" rel="nofollow">redis_exporter</a>,按照推荐进行设置。如果需要,大家可以看<a href="https://github.com/oliver006/redis_exporter" target="_blank" rel="nofollow">官方文档</a>进行相应的学习。</span></p> <p> </p> </div> </div> <div class="alert alert-success" role="alert"><p>来源:<code>oschina</code></p><p>链接:<code>https://my.oschina.net/u/4353184/blog/3652000</code></p></div></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/yml" hreflang="zh-hans">YML</a></div> <div class="field--item"><a href="/tag/buckets" hreflang="zh-hans">Buckets</a></div> <div class="field--item"><a href="/tag/tooltip" hreflang="zh-hans">tooltip</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> <div class="field--item"><a href="/tag/regex" hreflang="zh-hans">regex</a></div> <div class="field--item"><a href="/tag/gauge" hreflang="zh-hans">gauge</a></div> <div class="field--item"><a href="/tag/postfix" hreflang="zh-hans">postfix</a></div> </div> </div> Fri, 24 Apr 2020 00:19:22 +0000 こ雲淡風輕ζ 3590067 at http://www.e-learn.cn How can I make JsonResult return an array of arrays (without field names) rather than an array of objects? http://www.e-learn.cn/topic/3410630 <span>How can I make JsonResult return an array of arrays (without field names) rather than an array of objects?</span> <span><span lang="" about="/user/61" typeof="schema:Person" property="schema:name" datatype="">笑着哭i</span></span> <span>2020-02-22 04:06:48</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have an IEnumerable list of date/value pairs that I am returning as a Json list to flot. However, when I call JsonResult(), the result looks like this:</p> <pre><code>[{"Date":date1, "Value":value1}, {"Date":date2, "Value":value2}...] </code></pre> <p>Flot is expecting</p> <pre><code>[[date1, value1], [date2, value2]...] </code></pre> <p>Is there any simple way to get the MVC framework to output objects like this or do I need to write my own seralizer / StringBuffer code? For that matter I don't even need to output the field names, just the values themselves.</p> <br /><h3>回答1:</h3><br /><p>It sounds like you just need to return a string like this:</p> <pre><code>var builder = new StringBuilder(); builder.Append("["); foreach (var item in listOfDateTimes) builder.AppendFormat("[{0}, {1}], ", item.Key, item.Value); var result = builder.ToString().TrimEnd(new char[]{',',' '}) + "]"; return result; </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>Are these date/value pairs type of <code>System.Web.UI.Pair</code>? If so, you can do this ;</p> <pre><code>return Json(yourIEnumerable.Select(x =&gt; new[] { x.First, x.Second }).ToArray()); </code></pre> <p>It returns the way you want it to be ;</p> <pre><code>[["\/Date(1255686550232)\/","foo"],["\/Date(1255686550232)\/","bar"]] </code></pre> <p>Even if they're not type of <code>System.Web.UI.Pair</code>, I'm sure you get the idea.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>Your best bet is to write it yourself, but it is a trivial exercise to do.</p> <p>In my mind, is it worth spending on hour looking for a way to do it, when you can spend 10 minutes and just do the serialization yourself. </p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/1573643/how-can-i-make-jsonresult-return-an-array-of-arrays-without-field-names-rather</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/aspnet-mvc" hreflang="zh-hans">asp.net-mvc</a></div> <div class="field--item"><a href="/tag/json" hreflang="zh-hans">json</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> </div> </div> Fri, 21 Feb 2020 20:06:48 +0000 笑着哭i 3410630 at http://www.e-learn.cn Create multidimensional object with multidimensional arrays http://www.e-learn.cn/topic/3387647 <span>Create multidimensional object with multidimensional arrays</span> <span><span lang="" about="/user/188" typeof="schema:Person" property="schema:name" datatype="">旧时模样</span></span> <span>2020-02-12 15:23:02</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Im trying to build a somewhat advanced "Flot" jQuery pluging graph. For that, I need a multidimensional object (or at least I think it is).</p> <p>The structure should look like this:</p> <pre><code>var datasets = { "usa": { label: "USA", data: [[1988, 483994], [1989, 479060], [1990, 457648], [1991, 401949], [1992, 424705], [1993, 402375], [1994, 377867], [1995, 357382], [1996, 337946], [1997, 336185], [1998, 328611], [1999, 329421], [2000, 342172], [2001, 344932], [2002, 387303], [2003, 440813], [2004, 480451], [2005, 504638], [2006, 528692]] }, "russia": { label: "Russia", data: [[1988, 218000], [1989, 203000], [1990, 171000], [1992, 42500], [1993, 37600], [1994, 36600], [1995, 21700], [1996, 19200], [1997, 21300], [1998, 13600], [1999, 14000], [2000, 19100], [2001, 21300], [2002, 23600], [2003, 25100], [2004, 26100], [2005, 31100], [2006, 34700]] } }; </code></pre> <p>From my code-behind i've generated some lists looking like this (Not the same data, but dont mind that):</p> <pre><code>&lt;ul id="MOBILISATION"&gt; &lt;li data-key="2012/3/27"&gt;02:10&lt;/li&gt; &lt;li data-key="2012/3/28"&gt;05:25&lt;/li&gt; &lt;li data-key="2012/3/29"&gt;09:21&lt;/li&gt; &lt;li data-key="2012/3/30"&gt;00:00&lt;/li&gt; &lt;li data-key="2012/3/31"&gt;00:00&lt;/li&gt; &lt;/ul&gt; &lt;ul id="OPERATIONS"&gt; &lt;li data-key="2012/3/27"&gt;19:51&lt;/li&gt; &lt;li data-key="2012/3/28"&gt;18:35&lt;/li&gt; &lt;li data-key="2012/3/29"&gt;14:39&lt;/li&gt; &lt;li data-key="2012/3/30"&gt;07:46&lt;/li&gt; &lt;li data-key="2012/3/31"&gt;10:26&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Where "USA/usa" is "MOBILISATION", "1988" is "2012/3/27" and "483994" is "02:10". You get picture!!??</p> <p>I've tried writing some jQuery, but it obviously doesnt work:</p> <pre><code> var objects = []; var array = []; var categoryName = ""; $('div#Container ul').each(function () { catName = $(this).attr('id'); $('li', this).each(function () { array.push([new Date($(this).data('key')).getTime(), $(this).text()]); }); objects.push({ categoryName: { label: categoryName, data: array} }); }); var datasets = objects; </code></pre> <p>As you can see i've used push to <code>objects</code> array. Clearly thats not giving me the result I want. Im kindda loosing whats up and down in this, as its the first time im working with javascript/jQuery objects. Whats the best solution for this?</p> <br /><h3>回答1:</h3><br /><p>Your datasets is an object, not an array - you can't push to it. Also, your variable <code>categoryName</code> does not work like that (read more on dot and bracket notation). Try this:</p> <pre><code>var datasets = {}; $('div#Container ul').each(function () { catName = this.id; var array = []; $('li', this).each(function () { array.push([ new Date($(this).data('key')).getTime(), $(this).text() ]); }); datasets[catName.toLowercase()] = { label: catName.toUppercase(), data: array }; }); </code></pre> <p>Also, I'm not sure whether you really would need to create <code>Date</code> objects, but that depends on your in- and output format.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>Change <code>var objects = [];</code> to <code>var objects = {};</code> and change </p> <pre><code>objects.push({ categoryName: { label: categoryName, data: array} }); </code></pre> <p>to</p> <pre><code>objects[categoryName] = { label: categoryName, data: array}; </code></pre> <p>The problem you have with JSON objects is in setting properties with a variable index. Yuo can use array notation to do that, as above.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>Try</p> <pre><code> var data = {}; $('div#Container ul').each(function() { var sub_obj = data[$(this).attr('id').toLowerCase()] = { label: $(this).attr('id').toUpperCase(), data: [] }; $(this).children('li').each(function() { sub_obj.data.push([$(this).data('key'), $(this).text()]); }); }); console.log(data); </code></pre> <br /><br /><br /><h3>回答4:</h3><br /><pre><code>function convertObject(obj, key) { let returnObje = {}; if (!key) key = ""; else key = key + "_"; Object.keys(obj).forEach(function (k1) { if (obj[k1] != null &amp;&amp; typeof obj[k1] == "object") { returnObje = Object.assign({}, returnObje, convertObject(obj[k1], key + k1)) } else returnObje[key + k1] = obj[k1]; }); return returnObje; } var hotels = { "hilton": {"name": "hilton hotel" }, "newton": {"name": "newton hotel"} }; convertObject(hotels); </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/11648586/create-multidimensional-object-with-multidimensional-arrays</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> <div class="field--item"><a href="/tag/jquery" hreflang="zh-hans">jquery</a></div> </div> </div> Wed, 12 Feb 2020 07:23:02 +0000 旧时模样 3387647 at http://www.e-learn.cn Create multidimensional object with multidimensional arrays http://www.e-learn.cn/topic/3387642 <span>Create multidimensional object with multidimensional arrays</span> <span><span lang="" about="/user/59" typeof="schema:Person" property="schema:name" datatype="">╄→гoц情女王★</span></span> <span>2020-02-12 15:21:29</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Im trying to build a somewhat advanced "Flot" jQuery pluging graph. For that, I need a multidimensional object (or at least I think it is).</p> <p>The structure should look like this:</p> <pre><code>var datasets = { "usa": { label: "USA", data: [[1988, 483994], [1989, 479060], [1990, 457648], [1991, 401949], [1992, 424705], [1993, 402375], [1994, 377867], [1995, 357382], [1996, 337946], [1997, 336185], [1998, 328611], [1999, 329421], [2000, 342172], [2001, 344932], [2002, 387303], [2003, 440813], [2004, 480451], [2005, 504638], [2006, 528692]] }, "russia": { label: "Russia", data: [[1988, 218000], [1989, 203000], [1990, 171000], [1992, 42500], [1993, 37600], [1994, 36600], [1995, 21700], [1996, 19200], [1997, 21300], [1998, 13600], [1999, 14000], [2000, 19100], [2001, 21300], [2002, 23600], [2003, 25100], [2004, 26100], [2005, 31100], [2006, 34700]] } }; </code></pre> <p>From my code-behind i've generated some lists looking like this (Not the same data, but dont mind that):</p> <pre><code>&lt;ul id="MOBILISATION"&gt; &lt;li data-key="2012/3/27"&gt;02:10&lt;/li&gt; &lt;li data-key="2012/3/28"&gt;05:25&lt;/li&gt; &lt;li data-key="2012/3/29"&gt;09:21&lt;/li&gt; &lt;li data-key="2012/3/30"&gt;00:00&lt;/li&gt; &lt;li data-key="2012/3/31"&gt;00:00&lt;/li&gt; &lt;/ul&gt; &lt;ul id="OPERATIONS"&gt; &lt;li data-key="2012/3/27"&gt;19:51&lt;/li&gt; &lt;li data-key="2012/3/28"&gt;18:35&lt;/li&gt; &lt;li data-key="2012/3/29"&gt;14:39&lt;/li&gt; &lt;li data-key="2012/3/30"&gt;07:46&lt;/li&gt; &lt;li data-key="2012/3/31"&gt;10:26&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Where "USA/usa" is "MOBILISATION", "1988" is "2012/3/27" and "483994" is "02:10". You get picture!!??</p> <p>I've tried writing some jQuery, but it obviously doesnt work:</p> <pre><code> var objects = []; var array = []; var categoryName = ""; $('div#Container ul').each(function () { catName = $(this).attr('id'); $('li', this).each(function () { array.push([new Date($(this).data('key')).getTime(), $(this).text()]); }); objects.push({ categoryName: { label: categoryName, data: array} }); }); var datasets = objects; </code></pre> <p>As you can see i've used push to <code>objects</code> array. Clearly thats not giving me the result I want. Im kindda loosing whats up and down in this, as its the first time im working with javascript/jQuery objects. Whats the best solution for this?</p> <br /><h3>回答1:</h3><br /><p>Your datasets is an object, not an array - you can't push to it. Also, your variable <code>categoryName</code> does not work like that (read more on dot and bracket notation). Try this:</p> <pre><code>var datasets = {}; $('div#Container ul').each(function () { catName = this.id; var array = []; $('li', this).each(function () { array.push([ new Date($(this).data('key')).getTime(), $(this).text() ]); }); datasets[catName.toLowercase()] = { label: catName.toUppercase(), data: array }; }); </code></pre> <p>Also, I'm not sure whether you really would need to create <code>Date</code> objects, but that depends on your in- and output format.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>Change <code>var objects = [];</code> to <code>var objects = {};</code> and change </p> <pre><code>objects.push({ categoryName: { label: categoryName, data: array} }); </code></pre> <p>to</p> <pre><code>objects[categoryName] = { label: categoryName, data: array}; </code></pre> <p>The problem you have with JSON objects is in setting properties with a variable index. Yuo can use array notation to do that, as above.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>Try</p> <pre><code> var data = {}; $('div#Container ul').each(function() { var sub_obj = data[$(this).attr('id').toLowerCase()] = { label: $(this).attr('id').toUpperCase(), data: [] }; $(this).children('li').each(function() { sub_obj.data.push([$(this).data('key'), $(this).text()]); }); }); console.log(data); </code></pre> <br /><br /><br /><h3>回答4:</h3><br /><pre><code>function convertObject(obj, key) { let returnObje = {}; if (!key) key = ""; else key = key + "_"; Object.keys(obj).forEach(function (k1) { if (obj[k1] != null &amp;&amp; typeof obj[k1] == "object") { returnObje = Object.assign({}, returnObje, convertObject(obj[k1], key + k1)) } else returnObje[key + k1] = obj[k1]; }); return returnObje; } var hotels = { "hilton": {"name": "hilton hotel" }, "newton": {"name": "newton hotel"} }; convertObject(hotels); </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/11648586/create-multidimensional-object-with-multidimensional-arrays</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/javascript" hreflang="zh-hans">javascript</a></div> <div class="field--item"><a href="/tag/flot" hreflang="zh-hans">flot</a></div> <div class="field--item"><a href="/tag/jquery" hreflang="zh-hans">jquery</a></div> </div> </div> Wed, 12 Feb 2020 07:21:29 +0000 ╄→гoц情女王★ 3387642 at http://www.e-learn.cn