{"id":2070,"date":"2011-09-22T21:05:03","date_gmt":"2011-09-22T21:05:03","guid":{"rendered":"https:\/\/supportex.net\/?p=2070"},"modified":"2019-05-10T18:15:39","modified_gmt":"2019-05-10T16:15:39","slug":"rrd-python","status":"publish","type":"post","link":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/","title":{"rendered":"Using RRD with Python: short introduction"},"content":{"rendered":"<p><a href=\"http:\/\/oss.oetiker.ch\/rrdtool\/\">RRDtool<\/a> is a great facility which aims to replace MRTG and was written by Tobias Oetiker. RRDtool provides powerful features for collecting and visualizing various system metrics like network traffic, MySQL counters or whatever you want. It&#8217;s always good idea to know what is going on under the hood of your server. Managing servers we prefer to monitor its parameters. Here is a short introduction into using RRD with Python. We will use Fedora 15 \u00a0here but all examples should work in any another distributions as well. If you&#8217;re not familiar with RRD \u00a0you might need to check its <a href=\"http:\/\/oss.oetiker.ch\/rrdtool\/tut\/rrdtutorial.en.html\">tutorial<\/a>.<\/p>\n<p>At the beginning\u00a0 we need to install RRD library for using with Python:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">yum install rrdtool-python<\/code><\/p>\n<p>Now we can create RRD database with all sources we need. Please note that here we use 30 minutes intervals, but you can use any you want. We use two parameters as data source: <em>metric1<\/em> and <em>metric2<\/em>. It&#8217;s type is GAUGE. The thing is RRD supports different types (actually, Data Source Type): GAUGE, DERIVE, ABSOLUTE, COMPUTE or COUNTER. Probably, the most popular are COUNTER and GAUGE. The first one should be used for something\u00a0 like SNMP network interface counters for input and output traffic which increase constantly. And when you use just some parameters like HDD or CPU temperature you need to use GAUGE and RRD will gather data as is with no deltas. You can find other types description in RRD documentation.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">#!\/usr\/bin\/python\nimport rrdtool\nret = rrdtool.create(\"example.rrd\", \"--step\", \"1800\", \"--start\", '0',\n \"DS:metric1:GAUGE:2000:U:U\",\n \"DS:metric2:GAUGE:2000:U:U\",\n \"RRA:AVERAGE:0.5:1:600\",\n \"RRA:AVERAGE:0.5:6:700\",\n \"RRA:AVERAGE:0.5:24:775\",\n \"RRA:AVERAGE:0.5:288:797\",\n \"RRA:MAX:0.5:1:600\",\n \"RRA:MAX:0.5:6:700\",\n \"RRA:MAX:0.5:24:775\",\n \"RRA:MAX:0.5:444:797\")\n<\/pre>\n<p>Let&#8217;s consider all lines in details. First line include name of RRD database (&#8220;example.rrd&#8221;) and you can use here any path you want, step of parameters checking (30 minutes in our case), and the start point (0 or N means &#8216;now&#8217;). \u00a0&#8216;DS&#8217; in line 4-5 means Data Source, these lines include two our metrics. &#8216;2000&#8217; means that RRD can wait for 2000 seconds to get new values until it considers them as unknown (that&#8217;s is why we use 2000, which 200 seconds more of our 30 minutes interval). \u00a0Last two parameters &#8211; &#8216;U:U&#8217; &#8211; stand for min and max values of each metric (&#8216;unknown&#8217; in our case). Lines 6-13 describe what types of gained values RRD should store in its database. \u00a0It&#8217;s pretty self-describing (average and max values). \u00a0Mentioned values describe how many parameters RRD should keep. Considering it can be confusing I will omit explanation but note that these values were choosen\u00a0 to be <a href=\"http:\/\/oss.oetiker.ch\/rrdtool\/tut\/rrdtutorial.en.html\">compatible with MRTG<\/a> (actually, it&#8217;s not quite true\u00a0 since we use 1800 seconds periods and not 5 minutes, so you might need to modify it (if you also don&#8217;t use 5 minutes period) or keep like I did).<\/p>\n<p>To update your RRD database use next example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from rrdtool import update as rrd_update\nret = rrd_update('example.rrd', 'N:%s:%s' %(metric1, metric2));\n\n'N' also means 'now'. It should be launched every 30 minutes by cron, for instance. After some time RRD is ready to graph our data for a day, a week and a\u00a0 month\nimport rrdtool\nfor sched in ['daily' , 'weekly', 'monthly']:\n\n    if sched == 'weekly':\n        period = 'w'\n    elif sched == 'daily':\n        period = 'd'\n    elif sched == 'monthly':\n        period = 'm'\n    ret = rrdtool.graph( \"\/var\/www\/html\/metrics-%s.png\" %(sched), \"--start\", \"-1%s\" %(period), \"--vertical-label=Num\",\n         '--watermark=playground.in.supportex.net',\n         \"-w 800\",\n         \"DEF:m1_num=example:metric1:AVERAGE\",\n         \"DEF:m2_num=example.rrd:metric2:AVERAGE\",\n         \"LINE1:m1_num#0000FF:metric1\\r\",\n         \"LINE2:m2_num#00FF00:metric2\\r\",\n         \"GPRINT:m1_num:AVERAGE:Avg m1: %6.0lf \",\n         \"GPRINT:m1_num:MAX:Max m1: %6.0lf \\r\",\n         \"GPRINT:m2_num:AVERAGE:Avg m2: %6.0lf \",\n         \"GPRINT:m2_num:MAX:Max m2: %6.0lf \\r\")\n<\/pre>\n<p>You should get something like:<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"rrd-example\" src=\"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png\" alt=\"\" width=\"497\" height=\"176\" \/><\/p>\n<p>Various visualized system metrics always help us to provide better services in server management. Of course, you could use munin or Ganglia. But in some cases it can not be acceptable or probably you don&#8217;t want to write your own plugins. \u00a0That is where RRD can help.<\/p>\n<p><strong>Future reading<\/strong><\/p>\n<p>1. <a href=\"http:\/\/oss.oetiker.ch\/rrdtool\/tut\/rrdtutorial.en.html\">RRD tutorial<\/a> by\u00a0Alex van den Bogaerdt<br \/>\n2. <a href=\"http:\/\/cuddletech.com\/articles\/rrd\/index.html\">Getting Started with RRDtool <\/a> by\u00a0Ben Rockwood (this document\u00a0 is old but brilliant, definitely must read).<br \/>\n3. <a href=\"http:\/\/oss.oetiker.ch\/rrdtool\/doc\/index.en.html\">RRDtool Documentation<\/a><\/p>\n<p><strong>Didn&#8217;t find the answer to your question? <a href=\"\/contacts\/\">Ask it<\/a> our administrators to reply we will publish on website.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>RRDtool is a great facility which aims to replace MRTG and was written by Tobias Oetiker. RRDtool provides powerful features for collecting and visualizing various system metrics like network traffic, MySQL counters or whatever you want. It&#8217;s always good idea to know what is going on under the hood of your server. Managing servers we&hellip; <\/p>\n<div class=\"readmore-wrapper\"><a href=\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/\" class=\"more-link\">Read <\/a><\/div>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[275,217,243,244],"class_list":["post-2070","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-python-en","tag-rrd-en","tag-rrdtool-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using RRD with Python: short introduction &#8211; Supportex.NET blog<\/title>\n<meta name=\"description\" content=\"How to use RRD with Python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using RRD with Python: short introduction &#8211; Supportex.NET blog\" \/>\n<meta property=\"og:description\" content=\"How to use RRD with Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Supportex.NET blog\" \/>\n<meta property=\"article:published_time\" content=\"2011-09-22T21:05:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-10T16:15:39+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png\" \/>\n<meta name=\"author\" content=\"Oleksii Tykhonov\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oleksii Tykhonov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/\"},\"author\":{\"name\":\"Oleksii Tykhonov\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"headline\":\"Using RRD with Python: short introduction\",\"datePublished\":\"2011-09-22T21:05:03+00:00\",\"dateModified\":\"2019-05-10T16:15:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/\"},\"wordCount\":540,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png\",\"keywords\":[\"linux\",\"python\",\"RRD\",\"rrdtool\"],\"articleSection\":[\"linux\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/\",\"url\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/\",\"name\":\"Using RRD with Python: short introduction &#8211; Supportex.NET blog\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png\",\"datePublished\":\"2011-09-22T21:05:03+00:00\",\"dateModified\":\"2019-05-10T16:15:39+00:00\",\"author\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"description\":\"How to use RRD with Python\",\"breadcrumb\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#primaryimage\",\"url\":\"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png\",\"contentUrl\":\"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/supportex.net\/blog\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using RRD with Python: short introduction\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\",\"url\":\"https:\/\/supportex.net\/blog\/en\/\",\"name\":\"Supportex.NET blog\",\"description\":\"Server and network management company\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\",\"name\":\"Oleksii Tykhonov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"caption\":\"Oleksii Tykhonov\"},\"url\":\"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using RRD with Python: short introduction &#8211; Supportex.NET blog","description":"How to use RRD with Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/","og_locale":"en_US","og_type":"article","og_title":"Using RRD with Python: short introduction &#8211; Supportex.NET blog","og_description":"How to use RRD with Python","og_url":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/","og_site_name":"Supportex.NET blog","article_published_time":"2011-09-22T21:05:03+00:00","article_modified_time":"2019-05-10T16:15:39+00:00","og_image":[{"url":"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png","type":"","width":"","height":""}],"author":"Oleksii Tykhonov","twitter_misc":{"Written by":"Oleksii Tykhonov","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#article","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/"},"author":{"name":"Oleksii Tykhonov","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"headline":"Using RRD with Python: short introduction","datePublished":"2011-09-22T21:05:03+00:00","dateModified":"2019-05-10T16:15:39+00:00","mainEntityOfPage":{"@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/"},"wordCount":540,"commentCount":0,"image":{"@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#primaryimage"},"thumbnailUrl":"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png","keywords":["linux","python","RRD","rrdtool"],"articleSection":["linux"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/","url":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/","name":"Using RRD with Python: short introduction &#8211; Supportex.NET blog","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#primaryimage"},"image":{"@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#primaryimage"},"thumbnailUrl":"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png","datePublished":"2011-09-22T21:05:03+00:00","dateModified":"2019-05-10T16:15:39+00:00","author":{"@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"description":"How to use RRD with Python","breadcrumb":{"@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#primaryimage","url":"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png","contentUrl":"http:\/\/zer0.supportex.net\/blog\/wp-content\/uploads\/2011\/09\/rrd-example.png"},{"@type":"BreadcrumbList","@id":"https:\/\/supportex.net\/blog\/2011\/09\/rrd-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/supportex.net\/blog\/en\/"},{"@type":"ListItem","position":2,"name":"Using RRD with Python: short introduction"}]},{"@type":"WebSite","@id":"https:\/\/supportex.net\/blog\/en\/#website","url":"https:\/\/supportex.net\/blog\/en\/","name":"Supportex.NET blog","description":"Server and network management company","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251","name":"Oleksii Tykhonov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","caption":"Oleksii Tykhonov"},"url":"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/"}]}},"_links":{"self":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/comments?post=2070"}],"version-history":[{"count":2,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2070\/revisions"}],"predecessor-version":[{"id":3184,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2070\/revisions\/3184"}],"wp:attachment":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/media?parent=2070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/categories?post=2070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/tags?post=2070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}