Wednesday, April 25, 2012

How do I remove filenames with dashes?

This does not work....
ITASC $ rm '-z'
rm: illegal option -- z
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file
Simple: use the /- to depict dashes:
./CustomFunctions/.classpath
./-z
tibco@pghdbeengl01:~/build
$ rm ./-*

Wednesday, April 18, 2012

Apache Log Regex for simpleminded parsing.

The common log format would be
10.75.80.77 2 - - [17/Apr/2012:08:52:32 -0400] "GET /EASE/admin/login.jsf HTTP/1.1" 302 - "-" "Mozilla/5.0"
So here the regex to parse this...
my $expression =
qr/([0-9\.]+)\s+\d*\s?\-\s\-\s\[(\d+)\/(\w{3})\/(\d{4}):(\d+):.*\s[-+]\d+\]\s\"(POST|GET)\s(.+)\sHTTP\/(\d.\d)\"\s(\d{3})\s(\d+|-)\s\"(\S+)\"\s\"(.+)\"/x;
my (
 $all,       $ip,       $dayofmonth, $month,    $year,
 $hourofday, $reqtype, $req,        $protocol, $response,
 $size,      $ref,     $agent
) = @rec = $_ =~ /($expression)/i;
Here an example of yet another parser implementation of apache logs... that greps all the POST and GET's
use Data::Dumper;
use Text::Table;

sub line {
 my $x = shift;
 $x = $x ? $x : $W;
 for ( 1 .. $x ) { print "-"; }
 print "\n";

}

BEGIN {
 our $W = 70;
 %row = ();
 print "\n\n";
 line($W);
 printf "[%-35s]\n", $ARGV[$cc];
 line($W);
 $cc++;
}
my @rec =();
my $expression =
qr/([0-9\.]+)\s+\d*\s?\-\s\-\s\[(\d+)\/(\w{3})\/(\d{4}):(\d+):.*\s[-+]\d+\]\s\"(POST|GET)\s(.+)\sHTTP\/(\d.\d)\"\s(\d{3})\s(\d+|-)\s\"(\S+)\"\s\"(.+)\"/x;
my (
 $all,       $ip,       $dayofmonth, $month,    $year,
 $hourofday, $reqtype, $req,        $protocol, $response,
 $size,      $ref,     $agent
) = @rec = $_ =~ /($expression)/i;

#print $dayofmonth, "\n";
if ($!) {
 my ($approot) = $req =~ m!^\/(\w+)\/.*$!x;
 my ($refhost) = $ref =~ m!https?\:\/\/([a-z.]+)\/.+!x;
 my $v = sprintf "%s %s, %s", $month, $dayofmonth, $year;
 $row{$response}{'byreq'}{$v}{$req}++;
 my $hkey = sprintf "%s-%s", $approot, $refhost;
 my $dkey = sprintf "%s-%s", $hourofday,$hkey;
 #print Dumper(\@rec);

 #die "deeeee", $v;
 $row{$response}{'byreqref'}{$v}{$req}{$ref}++;
 $row{$response}{'byhodreq'}{$v}{$hourofday}++;
 $row{$response}{'byipsub'}{$v}{$ip}++;
 $row{$response}{'bycontext'}{$v}{$approot}++;
 $row{$response}{'byrefhost'}{$v}{$hkey}++;
 $row{$response}{'byrefhosthod'}{$v}{$dkey}++;

}
else {
 print "skiping $_\n";
}

Wednesday, March 14, 2012

Please Log Everything with a Log4j.xml to my screen....

It's sad to put this down however every time I need it, I forget the syntax... whats the proper syntax to answer my "please log everything to the screen" question...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
    <!-- 
         an appender is an output destination, such as the console or a file;
         names of appenders are arbitrarily chosen
    -->
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"  value="%d %-5p %c (%F:%L) %m%n"/>  

        </layout>
    </appender>
     <appender name="stderr" class="org.apache.log4j.ConsoleAppender">
        <param name="target" value="System.err"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d [%t][%F:%L] : %m%n" />
        </layout>
    </appender>
 
    <!-- the root category -->
        <!-- 
            all log messages of level "debug" or higher will be logged, unless defined otherwise 
            all log messages will be logged to the appender "stdout", unless defined otherwise 
        -->
<root>
        <priority value="ALL"/>
        <appender-ref ref="stdout" />
        <appender-ref ref="stderr" />
    </root>
</log4j:configuration>

Friday, February 3, 2012

Mac OSX - running xterm and X11 xhost: unable to open display

So alright you have Mac OSX. Yay I I have a VPN to the office. Yay II (ip vpn adapter: 172.18.146.250) I have oodles of linux boxes! Yay III The problem: One odd ball process on my linux will throw a X11 Graphics error so how to get around that.... Try 1: Do the standard export and xhost + trick open Terminal.app and run:
computer:~ ~$ export DISPLAY=172.18.146.250:0
computer:~ ~$ xhost +
xhost:  unable to open display "172.18.146.250:0"
computer:~ ~$ 

Thats not right... lets start the Xserver in background (X Server startcmd is 'X').... and do this again...
computer:~ ~$ X &
launch_msg("CheckIn") IPC failure: Operation not permitted
Xquartz: Unable to locate waiting server: org.x.X11
Xquartz: X11.app = /Applications/Utilities/X11.app/Contents/MacOS/X11
Xquartz: Starting X server: /Applications/Utilities/X11.app/Contents/MacOS/X11 --listenonly
..SCM BUILD BOX profile been setup..
================================================
X11.app: main(): argc=2
 argv[0] = /Applications/Utilities/X11.app/Contents/MacOS/X11.bin
 argv[1] = --listenonly
Waiting for startup parameters via Mach IPC.
X11.app: No launchd socket handed off, unsetting DISPLAY
X11.app: do_start_x11_server(): argc=1
 argv[0] = X
Xquartz starting:
X.Org X Server 1.4.2-apple56
Build Date: 20100624
(EE) XKB: Couldn't open rules file /usr/X11/share/X11/xkb/rules/base
(EE) XKB: Couldn't open rules file /usr/X11/share/X11/xkb/rules/base

computer:~ ~$
Try 2: Now open the xterm under the X11 Application that has been started...
bash-3.2$ export DISPLAY=172.18.146.250:0
bash-3.2$ xhost +
access control disabled, clients can connect from any host
bash-3.2$ 

Friday, January 20, 2012

Where Are My Open Files Go? lsof++

So where are my IO handles (open files) are being spend... here a oneliner First get the PID and ram everything of lsof thru the parser....

[wev@wad03 logs]$ /usr/sbin/lsof -p 21336 | perl -MData::Dumper -ne 'BEGIN { my %x=();} {@s=split /\s+/;$x{$s[4]}++;} END { print Dumper(\%x); }'
$VAR1 = {
          'IPv6' => 3,
          'unix' => 1,
          'DIR' => 2,
          'TYPE' => 1,
          'CHR' => 4,
          '0000' => 1,
          'REG' => 463,
          'sock' => 1
        };
[wev@wad03 logs]$ 

Wednesday, December 28, 2011

PDFTK find pdfs and collate them oneliner

Here a oneliner to merge PDF's together that are in a sub-directory structure. (Prerequisite: installed working version of pdftk)
$ find . -type f -name '*.pdf' | 
      perl -ne 'BEGIN {  my @c=() } 
          {     push @c, $_;$_=undef; } 
                END {my $n=65; foreach (@c) 
                { chomp; $x=$x.sprintf " %c=\"%s\"", $n,$_;$y=$y. sprintf " %c", $n; $n++ 
                }; 
                  print "\npdftk $x shuffle $y output collated.pdf;\n" 
          }'

Tuesday, December 27, 2011

KeyRemapForMacBook and Kinesis Classic Keyboad mapping private.xml

The KeyRemapFor4MacBook tool is excellent to map any key or mouse event to your needs. I have a Kinesis Classic Ergonomic keyboard (PS1) and wanted to use it in my new office setup. Love the keyboard, love the Mac however both together the do not play nice (plus I do not want to buy a new Kinesis with Multimedia keymapping) So here we are the following details for the private.xml for Kenisis Classic Using the following translation tables: Key Mapping The first thing is to identify the make and model of the keyboard that I have. Goto the "About This Mac" and click "More Info" and a detailed summary of all the hardware and software metadata pops up.
NOTE: scroll down for version 8 syntax which has slightly changed... Its a work in progress but here the full what I have defined as the private.xml
<?xml version="1.0"?>
<root>
  <devicevendordef>
    <vendorname>Kinesis</vendorname>
    <vendorid>0x0d3d</vendorid>
  </devicevendordef>
  <deviceproductdef>
    <productname>Classic</productname>
    <productid>0x001</productid>
  </deviceproductdef>
  <appdef>
    <appname>com.apple.finder</appname>
    <equal>com.apple.finder</equal>
  </appdef>
  <appdef>
    <appname>RDC</appname>
    <equal>com.microsoft.rdc</equal>
  </appdef>
  <appdef>
    <appname>TERMINAL</appname>
    <equal>com.apple.Terminal</equal>
  </appdef>
  <item>
    <name>Kinesis Bindings</name>
    <identifier>private.deviceproductdef</identifier>
    <device_only>DeviceVendor::Kinesis, DeviceProduct::Classic
    </device_only>
    <list>
      <item>
        <name>Keys for Kinesis</name>
        <not>TERMINAL</not>
        <not>RDC</not>
        <!-- Change the Kinesis Alt, TAB / Command to TAB -->
        <autogen>--KeyToKey-- KeyCode::TAB, ModifierFlag::OPTION_R,
          KeyCode::TAB, ModifierFlag::COMMAND_R
        </autogen>
        <!-- Change the Kinesis Alt, LeftArrow / Spaces Ctrl LeftArrow -->
        <autogen>--KeyToKey-- KeyCode::CURSOR_LEFT, ModifierFlag::OPTION_R,
          KeyCode::CURSOR_LEFT, ModifierFlag::CONTROL_R
        </autogen>
        <autogen>--KeyToKey-- KeyCode::CURSOR_RIGHT, ModifierFlag::OPTION_R,
          KeyCode::CURSOR_RIGHT, ModifierFlag::CONTROL_R
        </autogen>
        <!-- Change the Kinesis Ctrl-END / Ctrl-HOME to HOME or END -->
        <autogen>--KeyToKey-- KeyCode::HOME, ModifierFlag::COMMAND_R,
          KeyCode::HOME, ModifierFlag::FN
        </autogen>
        <autogen>--KeyToKey-- KeyCode::END, ModifierFlag::COMMAND_R,
          KeyCode::END, ModifierFlag::FN
        </autogen>
        <!-- Change the Kinesis END and HOME key to go to the begin and end of a 
          line -->
        <autogen>--KeyToKey-- KeyCode::HOME, KeyCode::CURSOR_LEFT,
          ModifierFlag::COMMAND_L
        </autogen>
        <autogen>--KeyToKey-- KeyCode::END, KeyCode::CURSOR_RIGHT,
          ModifierFlag::COMMAND_R
        </autogen>
        <!-- Change the Kinesis Alt-Left and Alt-Right key to go to forward or backwards 
          one word -->
        <autogen>--KeyToKey-- KeyCode::CURSOR_LEFT, ModifierFlag::COMMAND_R,
          KeyCode::CURSOR_LEFT, ModifierFlag::OPTION_L
        </autogen>
        <autogen>--KeyToKey-- KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_R,
          KeyCode::CURSOR_RIGHT, ModifierFlag::OPTION_R
        </autogen>
        <!-- Change the Kinesis Ctrls to Command buttons -->
        <autogen>--KeyToKey-- KeyCode::CONTROL_L, KeyCode::COMMAND_L
        </autogen>
        <!-- Change the Kinesis Ctrls to Command buttons -->
        <autogen>--KeyToKey-- KeyCode::CONTROL_R, KeyCode::COMMAND_R
        </autogen>
      </item>
      <item>
        <name>Kinesis Command Key under Alt for Terminal only</name>
        <only>TERMINAL</only>
        <autogen>--KeyToKey-- KeyCode::OPTION_R, KeyCode::COMMAND_R</autogen>
      </item>
    </list>
  </item>
</root>

Version 8 Syntax

With the release of v8.x of the KeyRemapForMacBook also the syntax changed... thus the example above won't work... here the v8 of the same mapping for Kinesis.
<?xml version="1.0"?>
<root>
 <devicevendordef>
  <vendorname>Kinesis</vendorname>
  <vendorid>0x0d3d</vendorid>
 </devicevendordef>
 
 <deviceproductdef>
  <productname>Classic</productname>
  <productid>0x001</productid>
 </deviceproductdef>
 
 <appdef>
  <appname>com.apple.finder</appname>
  <equal>com.apple.finder</equal>
 </appdef>
 
 <appdef>
  <appname>RDC</appname>
  <equal>com.microsoft.rdc</equal>
 </appdef>
 
 <appdef>
  <appname>ECLIPSE</appname>
  <equal>org.eclipse.eclipse</equal>
 </appdef>
 
 <appdef>
  <appname>TERMINAL</appname>
  <equal>com.apple.Terminal</equal>
 </appdef>
 
 <item>
  <name>Kinesis Bindings</name>
  <device_only>DeviceVendor::Kinesis, DeviceProduct::Classic</device_only>
  <item>
   <name>Keys for Kinesis</name>
   <appendix>Keys for Kinesis Keyboard on MacOSX</appendix>
   <not>TERMINAL</not>
   <not>RDC</not>
   <identifier>kinesis.mapping.basic</identifier>
   <!-- Change the Kinesis Alt, TAB / Command to TAB -->
   <autogen>__KeyToKey__ KeyCode::TAB, ModifierFlag::OPTION_R,
    KeyCode::TAB, ModifierFlag::COMMAND_R
   </autogen>
   <!-- Change the Kinesis Alt, LeftArrow / Spaces Ctrl LeftArrow -->
   <autogen>__KeyToKey__ KeyCode::CURSOR_LEFT, ModifierFlag::OPTION_R,
    KeyCode::CURSOR_LEFT, ModifierFlag::CONTROL_R
   </autogen>
   <autogen>__KeyToKey__ KeyCode::CURSOR_RIGHT, ModifierFlag::OPTION_R,
    KeyCode::CURSOR_RIGHT, ModifierFlag::CONTROL_R
   </autogen>
   <!-- Change the Kinesis Ctrl-END / Ctrl-HOME to HOME or END -->
   <autogen>__KeyToKey__ KeyCode::HOME, ModifierFlag::COMMAND_R,
    KeyCode::HOME, ModifierFlag::FN
   </autogen>
   <autogen>__KeyToKey__ KeyCode::END, ModifierFlag::COMMAND_R,
    KeyCode::END, ModifierFlag::FN
   </autogen>
   <!-- Change the Kinesis END and HOME key to go to the begin and end of 
    a line -->
   <autogen>__KeyToKey__ KeyCode::HOME, KeyCode::CURSOR_LEFT,
    ModifierFlag::COMMAND_L
   </autogen>
   <autogen>__KeyToKey__ KeyCode::END, KeyCode::CURSOR_RIGHT,
    ModifierFlag::COMMAND_R
   </autogen>
   <!-- Change the Kinesis Alt-Left and Alt-Right key to go to forward or 
    backwards one word -->
   <autogen>__KeyToKey__ KeyCode::CURSOR_LEFT, ModifierFlag::COMMAND_R,
    KeyCode::CURSOR_LEFT, ModifierFlag::OPTION_L
   </autogen>
   <autogen>__KeyToKey__ KeyCode::CURSOR_RIGHT, ModifierFlag::COMMAND_R,
    KeyCode::CURSOR_RIGHT, ModifierFlag::OPTION_R
   </autogen>
   <!-- Change the Kinesis Ctrls to Command buttons -->
   <autogen>__KeyToKey__ KeyCode::CONTROL_L, KeyCode::COMMAND_L
   </autogen>
   <!-- Change the Kinesis Ctrls to Command buttons -->
   <autogen>__KeyToKey__ KeyCode::CONTROL_R, KeyCode::COMMAND_R
   </autogen>
  </item>
  <item>
   <name>Kinesis Command Key under Alt for Terminal only</name>
   <only>TERMINAL</only>
   <identifier>kinesis.terminal</identifier>
   <autogen>__KeyToKey__ KeyCode::K, VK_OPTION,
    KeyCode::EQUAL, ModifierFlag::CONTROL_R</autogen>
   <!-- Change the Kinesis Ctrls to Command buttons -->
   <autogen>__KeyToKey__ KeyCode::CONTROL_R, KeyCode::COMMAND_R
   </autogen>
  </item>
  <item>
   <name>Kinesis Command Key under Alt for Eclipse only</name>
   <only>ECLIPSE</only>
   <identifier>kinesis.eclipse</identifier>
   <!-- Change the Kinesis Ctrl - Space to  buttons -->
   <autogen>__KeyToKey__ KeyCode::SPACE, VK_COMMAND,
        KeyCode::SPACE, ModifierFlag::CONTROL_L </autogen>
  </item>
  <!-- 
   org.eclipse.eclipse

   -->
 </item>
</root>