<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>biotext.org.uk &#187; perl</title>
	<atom:link href="http://biotext.org.uk/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://biotext.org.uk</link>
	<description>Not a typewriter</description>
	<lastBuildDate>Wed, 08 Sep 2010 17:47:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Posting to Twitter automatically using OAuth</title>
		<link>http://biotext.org.uk/posting-to-twitter-automatically-using-oauth/</link>
		<comments>http://biotext.org.uk/posting-to-twitter-automatically-using-oauth/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 12:44:30 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://biotext.org.uk/?p=502</guid>
		<description><![CDATA[Twitter recently switched off basic HTTP authentication, forcing developers to use the more complex (but hopefully safer) OAuth. There are lots of OAuth examples out there, but they all seem to focus on interactive apps, where the user is sent to Twitter to authenticate, and then the app uses the resulting access token to post [...]]]></description>
			<content:encoded><![CDATA[<p>Twitter <a href="http://blog.twitter.com/2010/06/switching-to-oauth.html">recently switched off basic HTTP authentication</a>, forcing developers to use the more complex (but hopefully safer) OAuth. There are lots of OAuth examples out there, but they all seem to focus on interactive apps, where the user is sent to Twitter to authenticate, and then the app uses the resulting <strong>access token</strong> to post on the user&#8217;s behalf.</p>
<p>However, for <a href="http://funcnet.eu/">FuncNet</a> we have a simple script running in a cron job, which posts a status message to <a href="http://twitter.com/FuncNet">@FuncNet</a> every so often. This runs without any supervision, so I was left scratching my head as to how I could obtain the access token and <strong>access token secret</strong> required to post. The app registration page at http://twitter.com/oauth_clients/details/<em>NNNNNN</em> for each app only shows its <strong>consumer key</strong> and <strong>consumer secret</strong> which are something different.</p>
<p>Eventually, thanks to <a href="http://search.cpan.org/dist/Net-Twitter/">Net::Twitter</a> developer Marc Mims on <a href="http://groups.google.com/group/twitter-development-talk/browse_thread/thread/160cb4d3f20ef61">this thread</a>, I discovered that there&#8217;s a whole separate page for each app at http://dev.twitter.com/apps/<em>NNNNNN</em>/my_token which has the access token and access token secret required for the app to post to its own account.</p>
<p>N.B. In the URLs above, <em>NNNNNN</em> corresponds to the numeric ID for your application. If you don&#8217;t know what this is, just go to http://dev.twitter.com/apps, click on the app name to get the consumer strings, and then click on My Access Token to get the access strings.</p>
<p>Once you have these, you can post like this (thanks again to Marc for example):</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">use</span> Net<span style="color: #339933;">::</span><span style="color: #006600;">Twitter</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$nt</span> <span style="color: #339933;">=</span> Net<span style="color: #339933;">::</span><span style="color: #006600;">Twitter</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>
        traits <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #000066;">qw</span><span style="color: #339933;">/</span>OAuth API<span style="color: #339933;">::</span><span style="color: #006600;">REST</span><span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        consumer_key        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$YOUR_CONSUMER_KEY</span><span style="color: #339933;">,</span>
        consumer_secret     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$YOUR_CONSUMER_SECRET</span><span style="color: #339933;">,</span>
        access_token        <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$YOUR_ACCESS_TOKEN</span><span style="color: #339933;">,</span>
        access_token_secret <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$YOUR_ACCESS_SECRET</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">$nt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">update</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Bob's your uncle!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Why you need 4 distinct incomprehensible strings in order to post a single tweet, I don&#8217;t know, but presumably it&#8217;s justified on security grounds. What <em>isn&#8217;t</em> justified is Twitter hiding two of them somewhere else entirely, and not linking to that place from the main OAuth page for the app.</p>
<p>Not useful, guys.</p>
<p><strong>UPDATE:</strong> It seems there are bigger problems with Twitter&#8217;s new process. This <a href="http://arstechnica.com/security/guides/2010/09/twitter-a-case-study-on-how-to-do-oauth-wrong.ars/">detailed ArsTechnica article</a> describes OAuth 1.0a as &#8220;an inelegant hack&#8221; and Twitter&#8217;s implementation of it as being &#8220;against all reason&#8221;.</p>
<p>If you&#8217;re having trouble with it, you&#8217;re not alone; it seems almost designed to cause problems for app developers, and particularly open-source app developers. The article&#8217;s well worth a read.</p>
]]></content:encoded>
			<wfw:commentRss>http://biotext.org.uk/posting-to-twitter-automatically-using-oauth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding the dimensions of a PostScript image</title>
		<link>http://biotext.org.uk/finding-the-dimensions-of-a-postscript-image/</link>
		<comments>http://biotext.org.uk/finding-the-dimensions-of-a-postscript-image/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 16:35:22 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[publishing]]></category>

		<guid isPermaLink="false">http://biotext.org.uk/?p=387</guid>
		<description><![CDATA[If I have images in .ps or .eps format, how can I tell how big they are? i.e. how big would they print out at &#8216;natural&#8217; size without any scaling? If they have a correct bounding box defined &#8212; more likely with EPS than PS files &#8212; this bit of Perl will show you the [...]]]></description>
			<content:encoded><![CDATA[<p>If I have images in .ps or .eps format, how can I tell how big they are? i.e. how big would they print out at &#8216;natural&#8217; size without any scaling?</p>
<p>If they have a correct bounding box defined &#8212; more likely with EPS than PS files &#8212; this bit of Perl will show you the width and height in cm:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Usage: measure.pl &lt;postscript-file&gt;</span>
<span style="color: #666666; font-style: italic;"># Or send it some data on stdin.</span>
<span style="color: #666666; font-style: italic;"># Assuming the BBox is correct, it'll tell you the </span>
<span style="color: #666666; font-style: italic;"># dimensions in cm.</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #000066;">defined</span> <span style="color: #009900;">&#40;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$line</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&amp;lt</span><span style="color: #339933;">;</span><span style="color: #0000ff;">&amp;gt</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$line</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^%%BoundingBox:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$llx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$lly</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$urx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$ury</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$4</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$x_pts</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$urx</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$llx</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$y_pts</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ury</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$lly</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$x_in</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$x_pts</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">72</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$y_in</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$y_pts</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">72</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$x_cm</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$x_in</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2.54</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$y_cm</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$y_in</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2.54</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span> <span style="color: #ff0000;">&quot;%.1f %.1f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$x_cm</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$y_cm</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">last</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Dead handy when preparing images for a paper.</p>
<p>PS if you are hazy about the difference between PS and EPS &#8212; like me &#8212; have a look at <a href="http://www.tailrecursive.org/postscript/eps.html">this article</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://biotext.org.uk/finding-the-dimensions-of-a-postscript-image/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Simple server status monitoring with PHP and Perl</title>
		<link>http://biotext.org.uk/simple-server-status-monitoring-with-php-and-perl/</link>
		<comments>http://biotext.org.uk/simple-server-status-monitoring-with-php-and-perl/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 16:05:59 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Research]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://biotext.org.uk/?p=229</guid>
		<description><![CDATA[Recently, one of our web servers fell victim to an apparent DoS attack, being hammered with hundreds of simultaneous dynamic page requests, far more than it&#8217;s specced to handle. To its credit, it stayed up, although it took about five minutes to log in via ssh, and when we spotted what was happening, the load [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, one of our web servers fell victim to an apparent DoS attack, being hammered with hundreds of simultaneous dynamic page requests, far more than it&#8217;s specced to handle. To its credit, it stayed up, although it took about five minutes to log in via ssh, and when we spotted what was happening, the load average was over 100 which I think is the most loaded I&#8217;ve ever seen. The offending IP address was at UCSD who do a lot of bioinformatics, so there is a chance it was a misguided attempt to scrape a lot of data from us, rather than an actual hostile act, but in any case the upshot is the same.</p>
<p>It worried me that there was no easy way to remotely check the machine&#8217;s health, so I hacked together a quick PHP page to report various vital statistics on demand &#8212; load average, memory usage, disk usage etc. &#8212; and a Perl monitor that can raise the alarm if anything exceeds safe bounds.</p>
<p><span id="more-229"></span>The status script itself really is dead simple, it looks like this:</p>
<pre class="brush: php">

&lt;?php
    # status.php -- very simple server status monitor
    header( &#039;Content-type: text/plain&#039; );
    # Get and display load average times 3
    $load = sys_getloadavg();
    echo &quot;LoadAverage1: $load[0]\n&quot;;
    echo &quot;LoadAverage5: $load[1]\n&quot;;
    echo &quot;LoadAverage15: $load[2]\n&quot;;
    # Get and display all sorts of memory usage info
    echo join( &#039;&#039;, file( &#039;/proc/meminfo&#039; ) );
    # Get and display disk usage percentages
    $df = `/bin/df`;
    foreach( split( &quot;\n&quot;, $df ) as $line )
    {
        if( preg_match( &quot;/(\d+%)\s+(\S+)$/&quot;, $line, $matches ) )
        {
            $fs = $matches[ 2 ];
            $usage = $matches[ 1 ];
            echo &quot;Usage_$fs: $usage\n&quot;;
        }
    }
    # Count running processes
    $procs = `/bin/ps -e|wc -l`;
    echo &quot;RunningProcesses: $procs\n&quot;;
?&gt;
</pre>
<p>It uses PHP&#8217;s built-in sys_getloadavg function to return the load average, df to get the disk usage, and ps and wc to count the number of processes running. These should work on any Unix-ish system (let me know if they don&#8217;t!). Also, it uses the /proc filesystem to read lots of metrics about memory use, and this is Linux-specific. It produces output that looks like this:</p>
<pre>LoadAverage1: 0.59
LoadAverage5: 0.4
LoadAverage15: 0.29
MemTotal:      2021816 kB
MemFree:        337592 kB
Buffers:         35408 kB
Cached:         286676 kB
SwapCached:       3152 kB
Active:        1115608 kB
Inactive:       102288 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:      2021816 kB
LowFree:        337592 kB
SwapTotal:     4192956 kB
SwapFree:      4185596 kB
Dirty:             304 kB
Writeback:           0 kB
AnonPages:      893740 kB
Mapped:         119900 kB
Slab:           256036 kB
PageTables:      21240 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:   5203864 kB
Committed_AS:  1433344 kB
VmallocTotal: 34359738367 kB
VmallocUsed:    280284 kB
VmallocChunk: 34359458043 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     2048 kB
Usage_/: 48%
Usage_/var: 9%
Usage_/tmp: 1%
Usage_/dev/shm: 0%
Usage_/export/people: 16%
Usage_/home/bsm: 79%
Usage_/LINUX/local64: 94%
Usage_/cath/opt: 13%
Usage_/cath/svnbin: 23%
Usage_/nfs/mail: 82%
Usage_/LINUX/local: 89%
RunningProcesses: 149</pre>
<p>The Perl monitoring script is a bit more complex, so I&#8217;ve made it available for download <a title="server status check script (Perl)" href="http://biotext.org.uk/static/server_status_check.pl.v0_1">here</a>. It lets you set up a config file with rules specifying named fields from the PHP script&#8217;s output, along with maximum and/or minimum allowable values for them. From the script&#8217;s comments:</p>
<pre>#!/usr/bin/perl

# server_status_check.pl
# Andrew Clegg
#
# This script parses the output of status.php and compares it to a
# list of minimum and maximum allowable values for server resources
# specified in a config file. The config file contains one rule per
# line, like so:
#
# min MemFree 7500000
# min Usage_/LINUX/local64 95
# max LoadAverage5 0.1
#
# Any line not in this format causes an error. Do not include any
# percent signs, units (e.g. kB) etc. in the config file; these
# are automatically stripped out from the results of status.php
# before applying the rules.
#
# For each resource that is lower than a min value or larger than
# a max value, a warning is printed. Also, if the config file
# contains any rules which name resources that are not found in
# the output of status.php at all, a warning is printed for each.
#
# It returns 0 if everything is fine, 255 if an error occurred, or
# the number of warnings issued if one or more of the resource
# rules are violated.</pre>
<p>You invoke the monitor script like this:</p>
<pre>./server_status_check.pl http://my.server/status.php my.config.file</pre>
<p>And it returns output that looks like this if anything rules from the config file are violated:</p>
<pre>MemFree has value 229472 which is less than minimum 250000
Usage_/ has value 86 which is greater than maximum 50
SomeIncorrectVariableName not found in server status report</pre>
<p>Since the return code is non-zero in case of a problem, you can easily use it in a cron job or shell script to take action when a server&#8217;s vital statistics move into dangerous ranges.</p>
<p>Of course, being PHP, you can use it from the command line for a quick summary of the local machine&#8217;s resources by just typing</p>
<pre>php status.php</pre>
<p>There are plenty more complex server monitoring tools out there, but you probably have to be a skilled sysadmin to use them, whereas these tools took a few hours to write, and five minutes to install. As usual, suggestions are welcome, and you are free to use them wherever and however you like, but please credit me and include a link back here.</p>
<p>Andrew.</p>
]]></content:encoded>
			<wfw:commentRss>http://biotext.org.uk/simple-server-status-monitoring-with-php-and-perl/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
