Tagged: linux RSS

  • Jeff Forman 12:42 pm on May 14, 2010 Permalink | Reply
    Tags: linux   

    Find, you are a dirty mistress 

    In my latest task at work, I have to write a script to take the most recent file from a particular directory changed within the last 240 minutes and copy it to a particular dated directory, in YYYYMMDDHHMM style.

    After some digging in the ‘find’ manual page and bothering a co-worker I present:

    find $directory -mmin -240 -name ‘foobarstring’ -printf “%p\n%CY%Cm%Cd%CH%CM\n”

    This prints out on two successive lines:
    $(filename)
    $(dated string in the date format above)

    The hardest part was getting the printf syntax right. When it works, it just works.

     
  • Jeff Forman 7:13 pm on October 21, 2009 Permalink | Reply
    Tags: linux, python, sqlite   

    Do or do not…the use of Try, with Python and sqlite 

    I’ve been hacking around writing random Django apps and scripts lately and came across something I had been meaning to look into over the past couple weeks. What happens when you select a row of data from a database (mysql, sqlite, whatever), that comes back empty and you try to iterate over it? Example: select * from authinfo where username = “foo”, and the user ‘foo’ does not exist.

    At first I thought:

    if db_query.fetchone()[0]:

    would work, but this only gives an error “TypeError: ‘NoneType’ object is unsubscriptable.”

    Then I remembered the try/except syntax, which worked great in this instance.

    #!/usr/bin/env python
    import sqlite3
    
    username = "testuser1"
    password = "foobar"
    
    conn = sqlite3.connect("/home/jforman/testsqlite.db")
    c = conn.cursor()
    db_query = c.execute("select username from authinfo where username = '%s'" % username)
    
    try:
     db_output = db_query.fetchone()[0].strip()
     print "output: %s" % db_output
    except TypeError:
     print "no user found"
    
    c.close()
    conn.close()

    Am I expecting too much that Python can handle iterating over an empty line (and just skip the whole thing) ? Or is this just how it’s done, where every database query-related line is done in a try stanza?

     
    • Michael Marineau 8:57 pm on October 21, 2009 Permalink | Reply

      It’s a little cleaner to use an if rather than a try/except:

      db_query = c.execute(“blah”)
      row = db_query.fetchone()

      if row:
      print “row 1: %s” % row[0]
      else:
      print “nothing found”

    • Steve Laniel 7:49 am on October 22, 2009 Permalink | Reply

      Right; I second Mike. Jeff, the thing to remember is that an empty list evaluates to False. That’s why “if row: foo” works.

      In C and C++ and Perl, you could compress those rather nicely:

      if( row = db_query.fetchone() ) {
      doSomethingWith(row);
      }
      else {
      doSomethingElse();
      }

      One happy consequence of putting the row inside the if() is that the row goes out of scope when you leave the if/else. Whereas in Python, that row is sticking around even when you’re done with it.

    • Jeff Forman 7:54 am on October 22, 2009 Permalink | Reply

      @Mike and @Steve,

      I could have swore I tried doing what you guys suggested and throwing it in an if/else block, but I got that type error. Yay computer languages. Good to know for next time.

  • Jeff Forman 12:48 pm on July 21, 2009 Permalink | Reply
    Tags: git, linux   

    Configuring Gitweb on Ubuntu 

    I’ve been digging into Git more lately as a revision control system for my personal stuff, and wanted a nice GUI way to visualize diffs in a browser. Enter Gitweb. I poked around and found bits and pieces of tutorials, but nothing specific for Ubuntu. So I present to you my step-by-step on how I got it working viewing repos.

    • Ensure you have a working Apache setup first.
    • Install the package: aptitude install gitweb
    • Edit your /etc/gitweb.conf. The most important setting in there is $projectroot, which is the parent directory of the Git repos you want to share.
    • Since I am not using a specific name-based virtual host for this, I just added the gitweb directory setup to the base Apache config. Since Ubuntu includes the entire /etc/apache2/conf.d dir, I just stuck the below in /etc/apache2/conf.d/gitweb:

    RewriteEngine on
    RewriteRule ^/gitweb/([a-zA-Z0-9_\-]+\.git)/?(\?.*)?$ /cgi-bin/gitweb.cgi/$1 [L,PT]

    Alias /gitweb /scratch
    <Directory /scratch>
    Options Indexes FollowSymlinks ExecCGI
    DirectoryIndex /cgi-bin/gitweb.cgi
    AllowOverride None
    </Directory>

    /scratch, used above, is the parent directory of my Git repositories.

    • The only gotcha I found in the installation, is that the gitweb.cgi by default expects its media related files, CSS, gifs, etc in the root directory of the URL. For simplicities sake, I just copied these files, found in  /usr/share/gitweb to /var/www. If you wish to change this, like most people will, just edit your gitweb.conf, which is found under /etc, and change the path to a place you expect the files to live.

    Hopefully this helps someone else setting up Gitweb. I am sure there are some security enhancements I could make surrounding Gitweb, but since this is a simple home installation, I did not go down that route.

     
    • kikito 3:35 am on August 2, 2010 Permalink | Reply

      Instead of copying the files around, you can try this:

      Alias /gitweb.css /usr/share/gitweb/gitweb.css
      Alias /git-logo.png /usr/share/gitweb/git-logo.png
      Alias /git-favicon.png /usr/share/gitweb/git-favicon.png

  • Jeff Forman 7:30 pm on March 25, 2009 Permalink | Reply
    Tags: linux, router   

    New Router, Asus WL-500gP2 and Tomato 

    Last Sunday, I woke up to a dead Internet connection. Gmail, Cnn, ESPN, all wouldn’t load. Great. Checked the laptop wireless, no connection. Even better. Power cycled the cable modem and connected the laptop directly to it, and voila, I was back on the Internet. A dead Linksys WRT54G on my hands. It had served me well, but being a couple years old and not having the specs to run any of the newer versions of DD-WRT or Tomato.  I went to Newegg and checked out a new one to buy.

    I settled on the Asus WL-500g Premium V2. While it was one of the more expensive ones (around $90), it had a serious amount of RAM and flash space. This allows for the bigger distributions of Linux to be installed which have more functionality. Being in Boston, normal Newegg shipping takes two days to get here.

    It came in, I logged into the router, and tried to load the Tomato firmware via the ‘Firmware Upgrade’ in the router’s web UI. The firmware file I tried was tomato.trx of the Tomato Linux firmware. The router complained about the firmware, saying the file was invalid. This is when I dreaded, did I download the wrong one? Will it not work?

    I jumped back on the Tomato page and noticed that there was another download, a tomato-ND.trx that was for another Asus model, the WL-520. I tried this one via the same method above, and didn’t work. Further down I saw the tftp explanation, where you reset the router with a combination of holding in certain buttons and then send the file in. Running Linux,  I sent the file via:

    atftp --verbose --trace -p -l tomato-ND.trx 192.168.1.1

    This worked, file completed sending. I waited a couple minutes and didn’t see anything happen. With a great deal of trepidation, I hit the reset button, waited a minute or two, and I was greeted with the Tomato admin UI when I tried to browse to http://192.168.1.1.

    So far I am pretty happy with it. I’ve been taking advantadge of the static DHCP and the dnsmasq functionality to be able refernece all my machines on the internal network by DNS name. The graphs of bandwidth usage are nice to see, but I don’t particularly use them much. Otherwise I use port forwarding to forward a single port to an internal machine for remote access.

    Hopefully this helps others who use this combination of hardware and software.

     
  • Jeff Forman 7:12 am on February 10, 2009 Permalink | Reply
    Tags: linux, nagios, raid   

    RAID array monitoring at home 

    Ever since I started using a simple RAID-1 setup at home, the only verification I had done to check on the status of the array was issuing some commands by hand on the command line.  I finally got around to implementing something a bit more proactive using Nagios, its NRPE plugin tool, and a simple bash script that queries dmraid.

    To cut to the chase, for those who understand Nagios syntax, the below two sections are added to my server’s Nagios configs.

    define command {
    command_name    check_nrpe
    command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
    }

    define service {
    use  generic-service
    name check-raid
    host_name $yourhosts
    service_description Software Raid
    check_command check_nrpe!check_raid
    }

    Servers who wish to have their RAID status checked, are added to the host_name parameter here, or can get the services tag added in the hostname definition.

    The below code is added client-side to the nrpe.cfg, where the NRPE daemon is installed and listening. The command section assigns the check_raid command a simple script to run when the test is executed.

    pinotnoir:/etc/nagios$ cat nrpe_local.cfg
    command[check_raid]=/usr/lib/nagios/plugins/monitor_raid.sh

    The script:

    pinotnoir:/usr/lib/nagios/plugins$: cat monitor_raid.sh
    #-----------------
    #!/bin/sh
    raid_status=`dmraid -s | grep status | awk '{print $3}'`
    if [ "$raid_status" = "ok" ] ; then
    echo "RAID Status OK"
    exit 0
    else
    echo "RAID Status NOT OKAY"
    exit 1
    fi

    This is the actual source of the script. As of the writing of this blog post, it only supports checking one RAID array using the dmraid utility.

    Example output of dmraid is pasted below, where my script simple pulls out the status line.

    pinotnoir:/usr/lib/nagios/plugins$ sudo dmraid -s
    *** Active Set
    name : nvidia_bfecfffb
    size : 976773120
    stride : 128
    type : mirror
    status : ok
    subsets: 0
    devs : 2
    spares : 0

    Understandably it is quite short and basic at this point, where I hope to have more scripts forth coming that provide more exposure into the status of the RAID array, and potentially support multiple arrays.

     
    • Osgpcq 9:10 am on August 4, 2010 Permalink | Reply

      Just try to disconnect a sata drive and the status still is ok (I have log about the de-connection of the drive).

c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel