Archive

Posts Tagged ‘PHP’

Parsing Nagios status.dat in PHP

February 21st, 2010

If you’re just looking for the script or PHP module, you can get them via Subversion at: http://svn.jasonantman.com/nagios-xml/.

A while ago (back in late 2008), I wrote a PHP script that parses the Nagios status.dat file into an associative array. My original use was to output XML which was then read by another script on another server and used for a small custom GUI. It’s a very simple PHP script that just takes the path of the status.dat file (which, obviously, must be readable by the user running the script).

At that time, I was using Nagios v2. Since then, I’ve moved to Nagios v3, and have updated the script to include the ability to parse v3 status.dat files, as well as a function to detect the version of a status file. I also refactored the code so that the parsing functions are all contained in a single file (statusXML.php.inc) which is safe to include in other scripts. The actual statusXML.php file now just includes examples of how to call all of the functions and output XML (though it is equally useful to output the serialized array, or use it directly).

Since I posted my script online, two people have been kind enough to send back their modifications:

Both of these generous contributions have been included in my Subversion repository as of the current revision, 5. Unfortunately, due to my delay in putting my Nagios3 code into svn, both of these contributions are Nagios v2 only.

As time permits, I plan on merging Artur’s changes into the current version of statusXML.php.inc. Unfortunately, C isn’t one of my strong points, but I plan on also updating Whitham’s PHP module code to work with Nagios3 as soon as possible.

Stay tuned for updates, and thanks to both gentlemen for contributing their work. I’m always interested in hearing how people are using my code, and how they are making it better.

Also: While I added this project to Nagios Exchange, and plan on adding it to Monitoring Exchange, I don’t always keep those sites up to date (I can’t access Nagios Exchange right now, and who knows if I’ll have time to update it tomorrow). I strongly recommend directly checking out from Subversion at http://svn.jasonantman.com/nagios-xml/ or taking a look at the code through ViewVC at http://viewvc.jasonantman.com/cgi-bin/viewvc.cgi/nagios-xml/.

Projects , ,

Using Google Maps to produce usable, printable maps

December 1st, 2009

This is a follow-up to my Making maps from GIS data with Inkscape post. After playing around with Inkscape for quite a while, and coming up with the dismal results seen in that post, I decided there has to be an easier way. A little Googling turned up this video tutorial on how to print large scale maps from Google Maps. It turns out that the Google Maps API will honor almost any pixel resolution that it’s passed. The Screengrab add-on for Firefox has the wonderful capability of being able to capture a screengrab of page content, at actual resolution, regardless of screen resolution. So load up a 5000×5000 pixel Google Map, use the Screengrab addon, and end up with a full 5000×5000 pixel image file.

After testing this a bit, I decided to go the Google Maps route. This also has a lot of other added bonuses – I can store my overlay data in simple XML files, add and remove layers on-the-fly, and also make it available online (and, theoretically, to any Google Maps-equipped device used by responders). This even opens up the possibility of using paper maps as a last resort, and providing the Fire Department with live hydrant maps on GPS-enabled handheld devices and phones.

The quirks, however, may need some serious photoshopping (err, rather, gimping) to fix:

  1. With all of the background color, how will this look when printed?
  2. How do I make the town borders easily defined? It would be a lot of raster editing to remove the background color of areas outside of town.
  3. How do I overlay a grid for a street name index?

The first step was to setup a large Google Map to develop with. I used PHP and Monte Ohrt’s GoogleMapAPI PHP wrapper class. It was simple enough to setup a big (3300×5100px) map, zoom out in Firefox, and start adding some stuff. My examples and development pages, if you want to take a peek at the code, are here.

The first step was to draw a polygon for the outline of the town. I found some very detailed information on how to get zip code boundary lines on Matt Cutts’ blog. Apparently, he’s a Google software engineer, heading up their webspam team. I grabbed the files from the Census, as described, and came up with the boundary for my zip code looking like:

        60      -0.741427638843858E+02       0.409963180802469E+02
      -0.741375870000000E+02       0.410075970000000E+02
      -0.741308870000000E+02       0.410061970000000E+02
      -0.741308870000000E+02       0.410061970000000E+02
      -0.741307260000000E+02       0.410032600000000E+02
      -0.741326870000000E+02       0.409955970000000E+02
      -0.741278870000000E+02       0.409943970000000E+02
      -0.741280870000000E+02       0.409938970000000E+02
      -0.741327870000000E+02       0.409853970000000E+02
      -0.741352870000000E+02       0.409830970000000E+02
      -0.741369600000000E+02       0.409818620000000E+02
      -0.741410520000000E+02       0.409821940000000E+02
      -0.741412870000000E+02       0.409826970000000E+02
      -0.741412870000000E+02       0.409826970000000E+02
      -0.741417870000000E+02       0.409847970000000E+02
      -0.741427870000000E+02       0.409863970000000E+02
      -0.741482870000000E+02       0.409868970000000E+02
      -0.741536880000000E+02       0.409899970000000E+02
      -0.741510880000000E+02       0.409929970000000E+02
      -0.741531880000000E+02       0.409965970000000E+02
      -0.741571880000000E+02       0.409988970000000E+02
      -0.741557880000000E+02       0.410013970000000E+02
      -0.741461870000000E+02       0.410018970000000E+02
      -0.741400870000000E+02       0.410065970000000E+02
 
      -0.741375870000000E+02       0.410075970000000E+02
END

As per Matt’s instructions, I stripped off the first and last lines, converted everything to normal decimal notation, and built it into a PHP array:

$MP_boundary = array();
$MP_boundary[] = array(-74.137587, 41.007597);
$MP_boundary[] = array(-74.130887, 41.006197);
$MP_boundary[] = array(-74.130887, 41.006197);
$MP_boundary[] = array(-74.130726, 41.003260);
$MP_boundary[] = array(-74.132687, 40.995597);
$MP_boundary[] = array(-74.127887, 40.994397);
$MP_boundary[] = array(-74.128087, 40.993897);
$MP_boundary[] = array(-74.132787, 40.985397);
$MP_boundary[] = array(-74.135287, 40.983097);
$MP_boundary[] = array(-74.136960, 40.981862);
$MP_boundary[] = array(-74.141052, 40.982194);
$MP_boundary[] = array(-74.141287, 40.982697);
$MP_boundary[] = array(-74.141287, 40.982697);
$MP_boundary[] = array(-74.141787, 40.984797);
$MP_boundary[] = array(-74.142787, 40.986397);
$MP_boundary[] = array(-74.148287, 40.986897);
$MP_boundary[] = array(-74.153688, 40.989997);
$MP_boundary[] = array(-74.151088, 40.992997);
$MP_boundary[] = array(-74.153188, 40.996597);
$MP_boundary[] = array(-74.157188, 40.998897);
$MP_boundary[] = array(-74.155788, 41.001397);
$MP_boundary[] = array(-74.146187, 41.001897);
$MP_boundary[] = array(-74.140087, 41.006597);
$MP_boundary[] = array(-74.137587, 41.007597);

Though this data doesn’t seem exactly 100% accurate (at least by my knowledge of the town, and every map I can find) it’s quite close and a very good start.

I’ll update later this week when I have some more done…

Tech HowTos , , , , ,

WordPress… Finally!

December 17th, 2008

Well, I finally bit the bullet. I woke up this morning, got in to work (slow day) and said to myself, “My blog’s finally moving to WordPress. Today.” So, I read through the docs (really psyched about the Blogger import feature), downloaded 2.7 (latest), setup the DB and installed away. Once I did the preliminary things like changing the admin password and setting up some categories, I set about importing from Blogger.

Here was the first of the major issues. Though WordPress has really hyped their Blogger importer, they failed to mention that it is totally useless if you are self-hosting Blogger and publishing via FTP or SFTP, as I was. So, after a lengthy consultation with the great oracle of Google, I dedided I had two options – switch Blogger to publish to BlogSpot.com and use the WP import tool, or custom hack a script. I must admit, it felt a bit pointless… all of my old posts were in a directry at the same level as WP, it seemed quite stupid to have to jump through hoops to get that data. Though I’m not quite sure whether the GData API really doesn’t give access to posts if self-hosted.

I migrated publishing of Blogger to BlogSpot.com (foobarthudgrunt.blogspot.com), despite worries about confusing Google or messing up the little bit of ranking I’ve been able to gain. It successfully imported all 128 of my entries, and the few comments. But, as I navigated to the “Edit” page to have a look, an even bigger problem was apparent. All of the tags from Blogger had ended up as categories in WP. So I had no tags for any of my posts, and a gazillion categories with only one post in them. Luckily, the DB schema is pretty sane, and I qickly figured out that both categories and tags are stored in the wp_term_taxonomy table, and the difference between a category and a tag is simply that the taxonomy field is either “category” or “post_tag”, respectively. So, since I hadn’t added any posts in WP yet, I just changed taxonomy to “post_tag” for anything with an ID past the categories I’d added. And it seems to have worked beautifully.

Up next, however, was the hard part: sitting down with my list of categories and sifting through 128 posts to categorize them. The biggest pain is the default edit posts table in the admin interface lists 15 posts per page, and with a cursory glance at the source I couldn’t for the life of me figure out where that limit is set.

Next up, I spent some time looking over the configuration options in WP, updating my About page, and listing things to do in the future (more static pages, blogroll and links, etc.).

I was finally ready to setup pretty URLs. And… sure enough… I clicked “submit” to apply some changes to the default blog URL (my real domain, as opposed to the DynDNS domain) and subtitle, and I got kicked out of the admin interface. Try as I might, I had no luck logging in. I then found that WordPress doesn’t log anything anywhere – no MySQL log and no error messages in the Apache error log. Wondeful. I spent about an hour looking through the source, figuring out how the auth method works, and trying to set an MD5 password. It was obviously apparent that the password in the DB for my one user (administrative user) was generated by PHPass, not MD5, but the auth function was evaluating anything >= 32 characters as PHPass, and the MD5 of my password was 32 characters, so that wouldn’t work. I tried the “forgot password” link with both username and email, but no mail was being sent. When I reached the 2-1/2 hour mark, I started instrumenting the login code with some error_log() lines to see what was up. I narrowed the problem down to the block of code starting at lin 48 in wp-include/user.php – specifically, according to the comments, no credentials were being passed but the cookie wasn’t set. And it’s silent in that case. So, at this point, I’m totally lost. I decided to clear all of my browser’s cookies and auth info, and try again. No dice. After nearly 3 hours, I decided to do a packet capture, and found the horribly simple reason. Somewhere in the code, WP is evaluating the “siteurl” or “home” values from wp_options, and using them instead of just doing relative links. As a result, when the form submits, Apache keeps returning a 302, and the form submission never makes it there. Hopefully this won’t create a problem when I transition from DynDNS to a real static IP and domain name.

Next I enabled pretty permalinks, enabled mod_rewrite in the vhost, and selected a new template for my blog (though I’m planning on doing some heavy customization). I didn’t want to leave the default template up for too long, in case Google’s heavy crawling of my site picked up all of the new content somehow… I ended up narrowing it down to three themes, coincidentally all designed by mg12: Blocks2, iNove or ElegantBox. I installed them all on my box and looked at iNove first, and it was love at first sight.

So, that’s where it stands right now. I finally have my blog on WordPress and running, and have a theme. The action plan for tonight includes:

  • Using RewriteRule and a PHP script to point all of the old Blogger URLs to the new WordPress installation.
  • Adding some new static content, and top-bar links to my other sites.
  • Refining the theme a bit, possibly?
  • Adding links to my sites, and to the blogroll.
  • Adding the buttons/plugins for del.icio.us, Digg, Slashdot, reddit, etc.
  • Redirecting my main blog page to WordPress.
  • Redirecting or linking my old feed locations to WordPress.

Projects , , ,

Practical PHP and MySQL

June 26th, 2008

I’m taking a summer course in Building Data Driven Websites – not that I thought I’d learn much in such a course at SCILS, but I’d like to graduate on time, and need the credits, and Bill Crosbie is just the type of rare teacher that can keep even me awake and interested. Our book is Practical PHP and MySQL: Building Eight Dynamic Web Applications (Amazon by Jono Bacon. Now, I know it’s not a real book like, say, ESA3 by Frisch, which has a healthy web presense. But this thing is all code and doesn’t even have a web site, let alone easy code downloads!

The book does come with a heavily customized Ubuntu LiveCD. However, when I popped it in my OpenSuSE workstation, I couldn’t really make much out of the CD – there was certainly no easy-to-find “this is the code” directory. Well, after some exploring, I mounted the SquashFS filesystem and poked around a bit. Strange… seems to only have one real user (root) and, though they claim this is a fully-functional LAMP server, no Apache or MySQL. Really weird. Well, after poking for a few minutes, I found the holy grail – /root/.bash_history was intact! Just a quick look through it with less and I found what I was looking for: /opt/lampp. It appears that the install is actually ApacheFriends’ LAMPP, or XAMPP for Linux (gotta wonder if the guy writing this book doesn’t even know how to install Apache… I’m sure XAMPP for Linux is more bloated than a customized build of Apache/MySQL/PHP from source, especially since it’s only being used to host 8 sample projects, so a lot could be left out).

Anyway, it appears that LAMPP is running in a chroot’ed environment. The actual sample code is rooted at /opt/lampp/htdocs/sites. It seems that all of the PHP files are also owned by root and chmod’ed 777! And the top-level index.php file makes use of absolute links, so obviously he never thought that someone may want to copy the sample code and use it on a real box.

I just can’t imagine someone who’s a beginner with Linux, let alone a Windows person, trying to get this source code onto a machine where they can actually play with it. And… to make the situation worse… the LiveCD has vi and vim, but no Emacs!!!! Eeeek!!

For anyone who needs it, I have the archive available on my site. For non-*nix people, you’ll need Gzip or an equivalent program to extract it.

Reviews , , , ,

Custom MediaWiki Sidebar; New Blog?

June 18th, 2008

As you may have noticed, some Firefox 3 buttons have popped up not only here on my blog, but also on my wiki. While adding the buttons to Blogger was a simple addition to the template, getting them in the sidebar of MediaWiki wasn’t exactly as easy (yeah, I’m considering the arduous project of moving my whole 102+ page wiki to Drupal or another good F/OSS CMS).

After some serious grepping through the source, and adding HTML comments to see where they appeared, I finally found a solution to add the button to the MediaWiki sidebar – though I’d really like it to appear below the search box (I guess that’s something for my to-do list). I’m using the MonoBook skin (though somewhat modified). I’m using “MonoBook nouveau”, and it should be the version that shipped with MW 1.10.1. In this version, I added the code around line 166. Specifically, this was added before the <div id="p-search" class="portlet"> line, and after the end of the foreach ($this->data['sidebar'] as $bar => $cont) loop. This threw the button in a box directly above the search box, and below all of my sidebar links.

The code looked something like:

      <?php } ?>      <!-- firefox link added to MonoBook.php by jantman 2008-06-18 -->      <div class='portlet' id='p-logos'>          <h5>Cool Stuff</h5>          <div class='pBody'>              <ul>                  <li><a href="http://www.spreadfirefox.com/node&id=238326&t=305" target="_blank"><img border="0" alt="Firefox 3" title="Firefox 3" src="http://sfx-images.mozilla.org/affiliates/Buttons/firefox3/110x32_best-yet.png"/></a></li>              </ul>           </div>      </div>      <!-- end firefox link -->      <div id="p-search" class="portlet">

In other news, I’m taking a Data Driven Websites class this summer (PHP/MySQL, but for some reason they switched to a Windows server… endless problems, and I can’t even edit with Nano on the server, let alone emacs). Our first project was to build a blog engine, which I’m working on right now. Anyway, it got me thinking… the one thing that Blogger is missing is the ability to post to a given category, and allow users to view or subscribe to a specific category (or everything). So I think I may look into writing something like that myself, if I can’t find a good alternative that’s already done and is F/OSS. Regardless, I’ll probably be keeping the Blogger template as well as (ugh) moving over all of my current posts, which Blogger chose to store in raw HTML. So there’s going to be a lot of parsing on my future…

PS – When I get a new blog engine, I’m also going to go for a slightly modified template that uses relative widths and placement – so that code, like the snippet here, fits the screen correctly.

Projects , , ,

Bug/Issue Tracking – Update / Review of Eventum

September 17th, 2007

So, after many hours of investigating potential bug/issue tracking packages, I have chosen one. And gone live with it, all in one weekend. After much evaluation, my final choice was Eventum from MySQL (I’m using the current version, 2.0.1). I will admit that it is not perfect. There are some features I wanted that weren’t there, and the documentation is awful. But it’s written in PHP.

That being said, it is very much a community-driven project. The support on the mailing list, both from other community members and from Bryan Alsdorf, has been wonderful. I had some issues viewing the help documents, specifically those in pop-ups, but I’ve been told that most new documentation has been moved to the wiki at the link above.

The project provides for advanced bug- and issue-tracking including time tracking, file attachments, and customizable statuses, priorities, and categories. It also provides email integration, both in terms of sending email alerts and opening issues (as well as updating and tracking) by parsing incoming emails. The email alerts worked out-of-the-box, but I did not configure parsing of emails.

One of my main requirements which was met perfectly by Eventum is its’ ability to easily handle multiple projects. It also has a built-in capability to allow anonymous issue submission (enabled on a project-by-project basis). You can define custom fields for issues on a project-by-project basis, and set them as required fields for either registered users, anonymous users, or both.

One feature that I found lacking was the possibility for a user to view all of the open issues assigned to them. Currently, all user interface is on a project-by-project basis. Therefore, listing of open issues is only available for the currently selected project. To cope with this, I hacked together a little PHP script that just queries the database for issues by user and displays it in a simple little page.

One of the major features about Eventum that caught my eye was integration with a version control system (SCM, as far as the Eventum docs are concerned). The feature list stated integration with CVS and SVN. When I actually looked into integrating it with CVS, however, the problems began. Firstly, the javascript-based help popup would not display anything, let alone the proper page. Installation was otherwise perfect under Apache2. I was forced to browse to the included HTML file manually and check it out. The overview seemed simple enough – throw a script in your CVSROOT loginfo file, update a few variables in the web-based Eventum configuration, and you’re off to the races. Reading on, I found that the installation page was a confusing jumble of references to a deprecated perl script and the current PHP script to call from loginfo. Furthermore, database access is provided by having the script called from loginfo parse the logging information and then *run a HTTP GET* on a local script served in the Eventum web directory. This added level of abstraction not only confuses me to no end, but also introduces the possibility for malicious users to insert data in the Eventum SCM database simply by visiting a well-known URL.

More importantly, the script provided to be called by loginfo seems to expect the old CVS logging format, not the new one being provided by my installation of CVS 1.12.12. While annoying, this ended up being a minor fix in the provided “process_cvs_commits.php” – I simply had to rewrite the argument parsing code so that it no longer expects the file, old version, and new version (%{sVv}) information to be space-separated on the command line in the form of s,V,v tuples, but expects everything to be space-separated. I should be cleaning up my fix a bit and submitting it for inclusion in the next release.

Once patched, CVS integration works perfectly. Simply append an identifier to the end of your commit log message, such as “(issue: 21)” or “(bug: 21)” and the commit will be automatically associated with the issue of that number. When viewing an issue, a list of associated CVS commits can be viewed.

It must be remembered that, as I have read, Eventum is used internally by MySQL. It is, therefor, a mature project that is well tested in one circumstance. I believe that it is mature and generally well-working (though I’ve heard reports that the 2.x tree isn’t as stable as the older versions, which are most likely still in use at MySQL). It must also be noted that the issue with CVS integration is most likely only with the newer CVS versions using the new logging format (I don’t know when the switch was made) and will probably not be noticed in older projects which have established CVS systems.

Now, for the opinion section. Eventum has thousands of features. I have detailed every issue that I have, which total about five. I found it to be a stable system, ready-to-run out of the box. Overall, I think it has the best feature set of the open-source bug tracking systems that I surveyed, which are probably most of the ones out there. It’s a great project which I’d recommend to anyone, though if you want more advanced features (like integration with CVS, or things not offered such as anonymous issue viewing) you should be comfortable with coding in PHP until someone makes patches available.

Projects, Reviews , , , ,

Poor PHP

March 21st, 2007

Well, as mentioned below, today I was doing a little research. On the futility of a Computer Science major. Specifically, on the fact that the overwhelming odds are that in a career in IT, system administration, or even web programming, I’ll probably never apply 90% of what I learn in CS courses.

So, I went looking for online certificate programs, knowing that most of the vendors’ programs run $500 or more just for testing. I came by eCertificates.com. And this brings me to another topic… I clicked on the “Software Programming” category, and was surprised to see no PHP test. On a hunch, I looked under “Web Design”, and sure enough, there it was.

I know this is a common misconception, and I know that because it plays so well with the web, and Apache, PHP has been pigeonholed as a web language. Maybe I’m just crazy, or maybe it’s just because I know PHP better than anything else. But PHP is NOT a web programming language. It is a programming language that happens to work very well with web applications.

Using fread() and fwrite() I can slam together a command-line menu-based PHP script for administration quicker than I could write the def’s for a Python script. I’ve done it, I have a number of PHP CLI-based scripts running on my machines. Database access with PHP is a no-brainer. And one day, when I get around to reading my “ancient” pile of books and learning NCurses, PHP will be right there with me.

So I guess it is just a fact of the modern web mentality. But PHP has myriad uses aside from web applications and web content handling. Sure, my web site is written in it. But one of my backup scripts is, too. And more importantly, if I’m working with a database that already has a PHP-based web frontend, PHP seems like the natural choice for a CLI-based administration backend.

Miscellaneous Geek Stuff , , , ,