Misc

Table of contents:

Permissions

Backup & restore file permissions:

getfacl -R /var/www/myweb > permissions.acl

setfacl --restore=permissions.acl

Size calculation

Calculate whatever total storage files/folders matching a regexp has

find /data/backup -regex '.*.war..*' -print0 | du --files0-from=- -ch | tail -1

Find open pipes

(find /proc -type l | xargs ls -l | grep pipe) 2>/dev/null

Find which PID occupies a port

fuser -v -n tcp 1111

List all child processes of a PID

ps --forest -o pid,tty,stat,time,cmd -g <PID>

Systemtap scripts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#! /usr/bin/env stap

global slabs

probe vm.kmem_cache_alloc {
        slabs [execname(), bytes_req]<<<1
}

probe timer.ms(10000)
{
        dummy = "";
        foreach ([name, bytes] in slabs) {
            if (dummy != name)
                if (bytes*@count(slabs[name, bytes]) > 100000)
                    printf("Name:%s\t\t\tTotal:%d\n", name, bytes*@count(slabs[name, bytes]));
            dummy = name;
        }
        delete slabs
        printf("\n-------------------------------------------------------\n\n")
}

Detect SLAB leaks

Run like: stap -v --all-modules kmem_alloc7.stp dentry

# This script displays the number of given slab allocations and the backtraces leading up to it.

global slab = @1
global stats, stacks
probe kernel.function("kmem_cache_alloc") {
        if (kernel_string($s->name) == slab) {
                stats[execname()] <<< 1
                stacks[execname(),kernel_string($s->name),backtrace()] <<< 1
        }
}
# Exit after 10 seconds
# probe timer.ms(10000) { exit () }
probe end {
        printf("Number of %s slab allocations by process\n", slab)
        foreach ([exec] in stats) {
                printf("%s:\t%d\n",exec,@count(stats[exec]))
        }
        printf("\nBacktrace of processes when allocating\n")
        foreach ([proc,cache,bt] in stacks) {
                printf("Exec: %s Name: %s  Count: %d\n",proc,cache,@count(stacks[proc,cache,bt]))
                print_stack(bt)
                printf("\n-------------------------------------------------------\n\n")
        }
}

Awk

Split lines which is 20 chars to 10 chars

awk 'length != 20 { print ; next ; }
    { printf "%s\n%s\n",substr($0,0,10),substr($0,11) }' file.txt