<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!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><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>5.2.5. Monitoring Reads and Writes to a File</title><link rel="stylesheet" type="text/css" href="Common_Content/css/default.css" /><link rel="stylesheet" media="print" href="Common_Content/css/print.css" type="text/css" /><meta name="generator" content="publican 2.8" /><meta name="package" content="Systemtap-SystemTap_Beginners_Guide-2.1-en-US-2.1-2" /><link rel="home" href="index.html" title="SystemTap Beginners Guide" /><link rel="up" href="mainsect-disk.html" title="5.2. Disk" /><link rel="prev" href="traceio2sect.html" title="5.2.4. I/O Monitoring (By Device)" /><link rel="next" href="inodewatch2sect.html" title="5.2.6. Monitoring Changes to File Attributes" /></head><body><p id="title"><a class="left" href="http://www.fedoraproject.org"><img src="Common_Content/images/image_left.png" alt="Product Site" /></a><a class="right" href="http://docs.fedoraproject.org"><img src="Common_Content/images/image_right.png" alt="Documentation Site" /></a></p><ul class="docnav"><li class="previous"><a accesskey="p" href="traceio2sect.html"><strong>Prev</strong></a></li><li class="next"><a accesskey="n" href="inodewatch2sect.html"><strong>Next</strong></a></li></ul><div xml:lang="en-US" class="section" id="inodewatchsect" lang="en-US"><div class="titlepage"><div><div keep-together.within-column="always"><h3 class="title" id="inodewatchsect">5.2.5. Monitoring Reads and Writes to a File</h3></div></div></div><a id="idm8530752" class="indexterm"></a><a id="idp11006824" class="indexterm"></a><a id="idm4923952" class="indexterm"></a><a id="idp37433944" class="indexterm"></a><a id="idp7630224" class="indexterm"></a><a id="idm619936" class="indexterm"></a><div class="para"> This section describes how to monitor reads from and writes to a file in real time. </div><div class="formalpara" id="inodewatch"><h5 class="formalpara">inodewatch.stp</h5> <pre class="programlisting">#! /usr/bin/env stap probe vfs.write, vfs.read { # dev and ino are defined by vfs.write and vfs.read if (dev == MKDEV($1,$2) # major/minor device && ino == $3) printf ("%s(%d) %s 0x%x/%u\n", execname(), pid(), probefunc(), dev, ino) } </pre> </div><div class="para"> <a class="xref" href="inodewatchsect.html#inodewatch">inodewatch.stp</a> takes the following information about the file as arguments on the command line: </div><a id="idm4365848" class="indexterm"></a><a id="idm7165560" class="indexterm"></a><a id="idp12472464" class="indexterm"></a><a id="idp10154072" class="indexterm"></a><div class="itemizedlist"><ul><li class="listitem"><div class="para"> The file's major device number. </div></li><li class="listitem"><div class="para"> The file's minor device number. </div></li><li class="listitem"><div class="para"> The file's <code class="command">inode</code> number. </div></li></ul></div><a id="idm3353248" class="indexterm"></a><a id="idp502528" class="indexterm"></a><a id="idm7410112" class="indexterm"></a><div class="para"> To get this information, use <code class="command">stat -c '%D %i' <em class="replaceable"><code>filename</code></em></code>, where <code class="command"><em class="replaceable"><code>filename</code></em></code> is an absolute path. </div><div class="para"> For instance: if you wish to monitor <code class="filename">/etc/crontab</code>, run <code class="command">stat -c '%D %i' /etc/crontab</code> first. This gives the following output: </div><pre class="screen">805 1078319</pre><a id="idp37657384" class="indexterm"></a><a id="idp8361168" class="indexterm"></a><a id="idm3141240" class="indexterm"></a><div class="para"> <code class="computeroutput">805</code> is the base-16 (hexadecimal) device number. The lower two digits are the minor device number and the upper digits are the major number. <code class="computeroutput">1078319</code> is the <code class="command">inode</code> number. To start monitoring <code class="filename">/etc/crontab</code>, run <code class="command">stap inodewatch.stp 0x8 0x05 1078319</code> (The <code class="command">0x</code> prefixes indicate base-16 values. </div><div class="para"> The output of this command contains the name and ID of any process performing a read/write, the function it is performing (i.e. <code class="command">vfs_read</code> or <code class="command">vfs_write</code>), the device number (in hex format), and the <code class="command">inode</code> number. <a class="xref" href="inodewatchsect.html#inodewatchoutput">Example 5.10, “inodewatch.stp Sample Output”</a> contains the output of <code class="command">stap inodewatch.stp 0x8 0x05 1078319</code> (when <code class="command">cat /etc/crontab</code> is executed while the script is running) : </div><div class="example" id="inodewatchoutput"><h6>Example 5.10. <a class="xref" href="inodewatchsect.html#inodewatch">inodewatch.stp</a> Sample Output</h6><div class="example-contents"><pre class="screen">cat(16437) vfs_read 0x800005/1078319 cat(16437) vfs_read 0x800005/1078319</pre></div></div><br class="example-break" /></div><ul class="docnav"><li class="previous"><a accesskey="p" href="traceio2sect.html"><strong>Prev</strong>5.2.4. I/O Monitoring (By Device)</a></li><li class="up"><a accesskey="u" href="#"><strong>Up</strong></a></li><li class="home"><a accesskey="h" href="index.html"><strong>Home</strong></a></li><li class="next"><a accesskey="n" href="inodewatch2sect.html"><strong>Next</strong>5.2.6. Monitoring Changes to File Attributes</a></li></ul></body></html>