Friday, April 30, 2010

linux: how to see which packages are taking the most disk space

Here's a little scriptfoo that's very useful when you're running out of disk space. When you need to reclaim hard drive real estate urgently by removing those useless "Engineering and Scientific" packages, run diskhoggers. Of course, it only works on RPM-based systems, so it's useless on Ubuntu/Debian etc, but there should be equivalents (e.g. using dpkg and/or synaptic apt-get) on those systems too.

alias diskhoggers='rpm -qa --qf "%10{SIZE}\t%{NAME}\n" | sort -n' 


Here's an example run on my system:

[root@noumPC ~]# diskhoggers | tail -n 30
15370931 bcel
15585349 gutenprint
15615584 gnome-applets
16021416 gcc
16574947 webkitgtk
16742780 nautilus
17773959 vim-common
18113087 firefox
18147692 libgweather
19693980 libicu
20174285 python
20250203 ghostscript
22082141 fedora-release-notes
22312309 kernel-devel
22371021 python-lxml
24697430 xulrunner
25032963 foomatic-db-ppds
25455524 libpurple
26376798 eclipse-jdt
29624088 perl
32984533 eclipse-platform
46820396 qt-x11
49233851 google-chrome-beta
49484310 valgrind
50371329 libgcj
80240654 kernel
84381565 kernel
85572888 java-1.6.0-openjdk
111799012 glibc-common
246952321 java-1.5.0-gcj-javadoc

linux: extremely useful BASH scriptfoo for investigating coretraces:

grep -E "(#0 |#1 )" [[FILENAME]]  | grep -vE "(kernel_vsyscall|pthread_cond_timedwait|nanosleep)"
[/name_of_coretrace_file][/filename]

Example:
[root@domain ~]# grep -E "(#0 |#1 )" coretrace_4.2_upgrade_crash | grep -vE "(kernel_vsyscall|pthread_cond_timedwait|nanosleep)"<br />#0  0x0040abc8 in _IO_vfscanf_internal () from /lib/libc.so.6<br />#1  0x0041a451 in vsscanf () from /lib/libc.so.6<br />#1  0x003afdeb in read () from /lib/libpthread.so.0<br />#1  0x003afdeb in read () from /lib/libpthread.so.0<br />#1  0x003afa0e in __lll_mutex_lock_wait () from /lib/libpthread.so.0<br />#0  0x00130244 in _fini () from /lib/libSegFault.so<br />#1  0x0011e5b2 in _dl_fini () from /lib/ld-linux.so.2<br />#1  0x003afdeb in read () from /lib/libpthread.so.0<br />#1  0x003afdeb in read () from /lib/libpthread.so.0<br />#1  0x003afdeb in read () from /lib/libpthread.so.0<br />#0  0x0040f948 in _IO_vfscanf_internal () from /lib/libc.so.6<br />#1  0x0041a451 in vsscanf () from /lib/libc.so.6<br />#1  0x003ea3d6 in kill () from /lib/libc.so.6<br />#1  0x003af365 in sem_timedwait () from /lib/libpthread.so.0<br />


Now you know which are the interesting threads - the ones that are not doing kernel_vsyscall() or pthread_cond_timedwait() or nanosleep() are printed as the output of this command-chain.

Thursday, April 29, 2010

vim: my ~/.vimrc file

set number
set incsearch
set shiftwidth=4
set tabstop=4
set autoindent
set smartindent
set paste
set expandtab
set showcmd
 
" set search highlighting on
set hls
 
set scrolloff=2
 
" Quit without fuss on :Q
:command -nargs=0 Quit :qa!

" Write without fuss on :W
:command -nargs=0 Write :w 
 
 
" fix the vim+backspace problem in cygwin - might NOT be needed on native linux shells!
set backspace=indent,eol,start
 
" set syntax highlighting on (for all possible file types)
syntax on
 
" always show current cursor position (row, column) at bottom right
set ruler
 
" choose colors that look good on a dark background, if possible
" set background=dark

" set more suitable colors for the line numbers
highlight LineNr gui=NONE guifg=black    guibg=grey 
highlight LineNr cterm=NONE ctermfg=darkgrey  ctermbg=grey


" This highlights the background in a subtle red for text that goes over the 80 column limit
" http://stackoverflow.com/questions/235439/vim-80-column-layout-concerns
" press F3 to toggle 80 column overlength highlighting
let ColHL='off'
highlight OverLength ctermbg=darkred ctermfg=white guibg=#592929
" match OverLength /\%81v.\+/

function! Toggle80ColumnHighlight()
    if g:ColHL == 'on'
        match OverLength //
        let g:ColHL='off'
    elseif g:ColHL == 'off'
        match OverLength /\%81v.\+/
        let g:ColHL='on'
    endif
endfunction

nnoremap  :call Toggle80ColumnHighlight()

" mark text after column 80 ( >= vim7.3 )
" set colorcolumn=80


" function to show color scheme in use
" source: http://stackoverflow.com/questions/2419624/how-to-tell-which-colorscheme-a-vim-session-currently-uses
function! ShowColorSchemeName()
    try
        echo g:colors_name
    catch /^Vim:E121/
        echo "default
    endtry
endfunction

" set a better search highlight colors
" http://stackoverflow.com/questions/7103173/vim-how-to-change-the-highlight-color-for-search-hits-and-quickfix-selection
highlight Search cterm=NONE ctermfg=white ctermbg=darkblue
highlight Search gui=NONE   guifg=white  guibg=darkblue
" set better incremental search highlight colors
highlight IncSearch cterm=NONE ctermfg=darkgreen ctermbg=grey
highlight IncSearch gui=NONE   guifg=darkgreen  guibg=grey

Sunday, April 11, 2010

linux: how to safely add an ssh-agent with default key upon login

# safely add key to ssh-agent on login

test=`ps -ef | grep ssh-agent | grep -v grep | awk '{print $2}' | xargs`

if [ "$test" = "" ]
then
# there is no agent running
if [ -e "$HOME/agent.sh" ]
then
# remove the old file
rm -f $HOME/agent.sh
fi
# start a new agent
ssh-agent | grep -v echo &> $HOME/agent.sh
ssh-add /root/.ssh/id_rsa
fi

test -e $HOME/agent.sh && source $HOME/agent.sh



Adapted from: http://drupal.star.bnl.gov/STAR/blog-entry/jeromel/2009/feb/06/how-safely-start-ssh-agent-bashrc

Friday, April 9, 2010

linux: mutt is so much better than mail

Here's how to send a email from the linux command line, attach files to it, and have it sent to multiple recipients! Much better than having to paste the contents of a config file or text file into the body of an email and then use the plain old "mail" command.

echo | mutt  -s interesting_logs_please_check  -a syslogs.tar.gz admin@domain.org, user1@domain.org, user2@domain.org


references:

http://www.shelldorado.com/articles/mailattachments.html
(excellent)

"Multiple recipients may also be specified by separating each address with the , delimiter"
(tested this and it works)
http://www.freebsd.org/doc/handbook/mail-agents.html