Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Tuesday, 24 February 2015

Finding JVM arguments easily from the shell


You know the score, you're asked 'oh just check what the Xmx is on that JVM'... easy huh...

$ pgrep -fl java
17506 /bin/bash /Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/nbexec --userdir /Users/someuser/Library/Application Support/NetBeans/8.0.2 --cachedir /Users/someuser/Library/Caches/NetBeans/8.0.2 --jdkhome -J-Dcom.apple.mrj.application.apple.menu.about.name=NetBeans -J-Xdock:name=NetBeans -J-Xdock:icon=/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/nb/netbeans.icns --branding nb --clusters /Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/nb:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/ergonomics:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/ide:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/extide:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/java:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/apisupport:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/webcommon:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/websvccommon:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/enterprise:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/mobility:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/profiler:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/python:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/php:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/identity:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/harness:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/cnd:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/dlight:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/groovy:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/extra:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/javacard:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/javafx: -J-Dnetbeans.importclass=org.netbeans.upgrade.AutoUpgrade -J-Dnetbeans.accept_license_class=org.netbeans.license.AcceptLicense -L-XX:MaxPermSize=384m -J-Xmx1024m -J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.java2d.dpiaware=true -J-Dsun.zip.disableMemoryMapping=true SHELL=/bin/bash
17758 /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java -Djdk.home=/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home -classpath /Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/boot.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/org-openide-modules.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/org-openide-util-lookup.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/org-openide-util.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/boot_ja.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/boot_pt_BR.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/boot_ru.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/boot_zh_CN.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-modules_ja.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-modules_pt_BR.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-modules_ru.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-modules_zh_CN.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-util-lookup_ja.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-util-lookup_pt_BR.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-util-lookup_ru.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-util-lookup_zh_CN.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-util_ja.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-util_pt_BR.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-util_ru.jar:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform/lib/locale/org-openide-util_zh_CN.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/lib/tools.jar -Dnetbeans.default_userdir_root=/Users/someuser/Library/Application Support/NetBeans -Dnetbeans.dirs=/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/nb:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/ergonomics:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/ide:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/extide:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/java:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/apisupport:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/webcommon:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/websvccommon:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/enterprise:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/mobility:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/profiler:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/python:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/php:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/identity:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/harness:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/cnd:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/dlight:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/groovy:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/extra:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/javacard:/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/javafx: -Dnetbeans.home=/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/platform -Dcom.apple.mrj.application.apple.menu.about.name=NetBeans -Xdock:name=NetBeans -Xdock:icon=/Applications/NetBeans/NetBeans 8.0.2.app/Contents/Resources/NetBeans/nb/netbeans.icns -Dnetbeans.importclass=org.netbeans.upgrade.AutoUpgrade -Dnetbeans.accept_license_class=org.netbeans.license.AcceptLicense -Xmx1024m -client -Xss2m -Xms32m -XX:PermSize=32m -Dapple.laf.useScreenMenuBar=true -Dapple.awt.graphics.UseQuartz=true -Dsun.java2d.noddraw=true -Dsun.java2d.dpiaware=true -Dsun.zip.disableMemoryMapping=true -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Users/someuser/Library/Application Support/NetBeans/8.0.2/var/log/heapdump.hprof org.netbeans.Main --cachedir /Users/someuser/Library/Caches/NetBeans/8.0.2 --userdir /Users/someuser/Library/Application Support/NetBeans/8.0.2 --branding nb

Did you see it?

It then sinks in, its all on one line! You can't grep Xmx (well you can I guess if you have grep with colour highlighting - believe me, not everyone gets that luxury in the workplace!). Its still gonna be fun to find in that lot!

Help is at hand...
$ pgrep -fl java | tr -s " " "\n" | grep "Xmx"
-J-Xmx1024m
-Xmx1024m

Or perhaps you want all the -arguments...?<

$ pgrep -fl java | tr -s " " "\n" | grep "^-"
--userdir
--cachedir
--jdkhome
-J-Dcom.apple.mrj.application.apple.menu.about.name=NetBeans
-J-Xdock:name=NetBeans
-J-Xdock:icon=/Applications/NetBeans/NetBeans
--branding
--clusters
-J-Dnetbeans.importclass=org.netbeans.upgrade.AutoUpgrade
-J-Dnetbeans.accept_license_class=org.netbeans.license.AcceptLicense
-L-XX:MaxPermSize=384m
-J-Xmx1024m
-J-client
-J-Xss2m
-J-Xms32m
-J-XX:PermSize=32m
-J-Dapple.laf.useScreenMenuBar=true
-J-Dapple.awt.graphics.UseQuartz=true
-J-Dsun.java2d.noddraw=true
-J-Dsun.java2d.dpiaware=true
-J-Dsun.zip.disableMemoryMapping=true
-Djdk.home=/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
-classpath
-Dnetbeans.default_userdir_root=/Users/someuser/Library/Application
-Dnetbeans.dirs=/Applications/NetBeans/NetBeans
-Dnetbeans.home=/Applications/NetBeans/NetBeans
-Dcom.apple.mrj.application.apple.menu.about.name=NetBeans
-Xdock:name=NetBeans
-Xdock:icon=/Applications/NetBeans/NetBeans
-Dnetbeans.importclass=org.netbeans.upgrade.AutoUpgrade
-Dnetbeans.accept_license_class=org.netbeans.license.AcceptLicense
-Xmx1024m
-client
-Xss2m
-Xms32m
-XX:PermSize=32m
-Dapple.laf.useScreenMenuBar=true
-Dapple.awt.graphics.UseQuartz=true
-Dsun.java2d.noddraw=true
-Dsun.java2d.dpiaware=true
-Dsun.zip.disableMemoryMapping=true
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/Users/someuser/Library/Application
--cachedir
--userdir
--branding

Using vi as a hex editor

I recently had an issue where a license key was unable to be transferred, and attempts to fix using vi/tr/sed (don’t laugh!) didn’t get the file into the correct shape. As it was clearly using a binary forma, I really needed a proper hex editor.

Luckily I was able to list the files with od -ax on two machines, so I knew what I wanted!

It turns out you can use vi as a hex editor - its a commonly known thing out on the internet as ever (my source was http://www.kevssite.com/2009/04/21/using-vi-as-a-hex-editor/).

:%!xxd turns your file into a hex editor style representation

:%!xxd -r turns your edited file back into a binary form which you can then save.

Also handy for editing firmware, binaries, libraries - although be careful, it does seem to add a newline that you’ll need to remove using sed/tr/etc.

Tuesday, 26 August 2014

Quick and dirty XML in shell

I found myself helping a colleague having fun trying to get various bits of XML data out of a WebSphere config file today, I knocked this up as a quick way to list the data and hopefully be adapted to his requirements. I'm sure theres better ways of doing this, and feel free to suggest improvements or links to other better ways to parse XML in shell (obviously there various ways with CPAN, or xmllint but as you may have gathered my current workplace is locked down with minimal amounts of useful tools!).


for each in $(cat ./test.xml | sed "s/</ </g;s/>/> /g" | tr -d "\n")
do
  #echo "INPUT: [$each]"
  # Create a plain text string version of each
  temp=$(echo $each | tr -d "<>")

  # Check if its an attrib or data, or an element name
  if [[ $(echo $each | cut -c 1-2) == "</" ]]; then
   #its an </end> tag
   echo "ELEMENT END: [$temp]"
  elif [[ $each == "/>" ]]; then
   #its a self contained <end /> tag
   echo "ELEMENT END: [$lastElement]"
  elif [[ $(echo $each | cut -c 1) == "<" ]]; then
   #if it starts with a < and isn't either of the above
   #then its a <begin> tag
   echo "ELEMENT BEGIN: [$temp]"
   lastElement=$temp
  else
   #its either data or an attribute
   attribValue=$(echo $temp | cut -d= -f2 )
   attribName=$(echo $temp | cut -d= -f1 )
   if [[ $attribValue != $attribName ]]; then
    #it must be an attribute on an element
    echo "AttribName: $attribName"
    echo "AttribVal : $attribValue"
   else
    #it must be data
    echo "Data: $each"
   fi
   unset attribName
   unset attribValue
  fi
done

Of course, if you do have tools like xmllint to hand...

To find a port in the server index file —


#!/bin/bash

WASBASE=${2:-"/opt/IBM/WebSphere"}


for each in $(find $WASBASE -name serverindex.xml | grep -iv Templates)
do
xmllint --shell $each <<<"xpath //specialEndpoints[contains(@endPointName,'$1')]/endPoint/@port" | grep "=" | cut -d"=" -f2
done

Wednesday, 12 March 2014

Collecting Oracle SQL output from a script

I recently had to pick up some data for documenting what connects to what, and the easiest way appeared to be to start at the databases and work back through the 3-tier setup.

Heres a rough and ready way to get the info, we'll talk about improving the output later.

for each_SID in $(grep -v ^# /etc/oratab | egrep -v "^\+ASM|^agent" | cut -d: -f1)
do
 echo "DATABASE: ${each_SID}"
 ORAENV_ASK=NO
 ORACLE_SID=${each_SID}
 ORACLE_HOME=$(grep ${ORACLE_SID} /etc/oratab | cut -d: -f2)
 export ORAENV_ASK ORACLE_SID ORACLE_HOME
 .  ${ORACLE_HOME}/bin/oraenv
 ${ORACLE_HOME}/bin/sqlplus / as SYSDBA << EOSQL
  select distinct username,machine from v\$session;
  quit
EOSQL
done > /tmp/my_file_of_guff


REMEMBER: heredoc syntax is a PITA. The closing EOSQL (or whatever you use has to be the first thing on the line, no whitespace. You can use <<-EOSQL and close with at tab and your string, e.g. <TAB>EOSQL. Thanks to here for reminding me about this. I've got some notes about prefixing your string with ! but I can't remember for the life of me what it does.

The first thing you'll notice about your output is that its horrible. If you've used SQLPlus before you know its a turd that really isn't acceptable for the 21st century and won't have been surprised. We can however make things a little easier on ourselves.

These should help with the output getting wrapped...

SET WRAP OFF
SET PAGESIZE 0

And should you need to output to different files per database, using our example above, we can use the spool command to send the output to a file. It's still not great, but its better.

for each_SID in $(grep -v ^# /etc/oratab | egrep -v "^\+ASM|^agent" | cut -d: -f1)
do
 echo "DATABASE: ${each_SID}"
 ORAENV_ASK=NO
 ORACLE_SID=${each_SID}
 ORACLE_HOME=$(grep ${ORACLE_SID} /etc/oratab | cut -d: -f2)
 export ORAENV_ASK ORACLE_SID ORACLE_HOME
 .  ${ORACLE_HOME}/bin/oraenv
 ${ORACLE_HOME}/bin/sqlplus / as SYSDBA << EOSQL
  SET WRAP OFF
  SET PAGESIZE 0
  SPOOL /tmp/${ORACLE_SID}.extract
  select distinct username,machine from v\$session;
  SPOOL OFF
  quit
EOSQL
done