Tuesday, November 24, 2015

Useful Docker oneliners


Display image virtual size in human-readable format:

sudo docker inspect <image> | grep -i VirtualSize | awk '{$2/=1000000; printf "%.2f MB\n",$2}'

Checking what command has produced specific Docker image layer:

docker inspect --format '{{ ((index .ContainerConfig.Cmd ) 0) }}' <layer_id>

Useful Docker-related links:

https://imagelayers.io/
https://github.com/larsks/dockerize

Monday, September 14, 2015

Show amount of open files per process


This one also displays process name, pid and number of open files:
lsof | awk '{ print $2 " " $1; }' | sort -rn | uniq -c | sort -rn | head -20

Monday, August 10, 2015

Backup Jenkins to S3

This way we will backup all except for SCM folders and jars. All Jenkins configs, jobs, logs and builds will be preserved, only built artifacts will be skipped

Backup command:
export GZIP=-9 && tar czfh - /var/lib/jenkins/ --exclude-vcs --exclude "archive" --exclude "target" --exclude "/var/lib/jenkins/docker" --exclude "/var/lib/jenkins/.m2/repository" | /usr/local/bin/aws s3 --region us-west-2 cp - s3://<bucket_name>/jenkins-backup.tar

 Restore command:
aws s3 cp s3://confyrm-jenkins-backups/jenkins-backup.tar - | tar -C / -zxf -


Wednesday, June 24, 2015

Example Logstash config to parse Java / Scala multiline logs (e.g. stacktraces) into ES


Java/Scala stack traces are multiline and usually it have the message starting from

Any line which isn't starting with '[' will be joined into previous one having '[' at the beginning

E.g. this works with Logstash 1.4.0+:

if [type] == "app_logs" {
    multiline {
      pattern => "^[^\[]"
      what => "previous"
    }
    grok {
      match => { "message" => "\[(?<app_log_timestamp>.+)] \[%{WORD:app_name}\] \[(?<thread_name>.+)\] \[(?<class_name>.+)\] \[(?<marker>[a-zA-Z]*)\] \[(?<transaction_id>.*)\] \[%{WORD:log_level}\]: ?%{GREEDYDATA:msg}" }
    }
    date {
      match => ["app_log_timestamp", "MM/dd HH:mm:ss:SSS", "ISO8601"]
      target => "@timestamp"
      add_tag => [ "timestamp_updated_w_log_value" ]
      remove_field => [ "app_log_timestamp" ]
    }
}

This works for all Java multiline logs, the only rule is to not start multiline log newlines from '['.

Useful link: http://logstash.net/docs/1.4.0.rc1/filters/multiline

Also starting from Logstash 1.2 there is a 'multiline' codec (http://logstash.net/docs/1.2.2/codecs/multiline). But I didn't get it work properly with Logstash 1.4. What have I tried:

input {
  file {
      codec => multiline {
        pattern => "^\s"
        what => "previous"
      }
    ..... file path and so on
  }
}

What issue did I met with 'multiline' codec: Java stacktraces were parsed without the very first line. E.g. in ES I was getting:

java.lang.RuntimeException: Exception while executing statement : An I/O error occurred while sending to the backend. errorCode: 0, sqlState: 08006 at ... [other stacktrace lines omitted]

Instead of expected:

[06/24 16:43:51:393] [app_name] [pool-99-thread-999] [ClassName] [smth0] [bar] [ERROR]: Cannot load XXX java.lang.RuntimeException: Exception while executing statement : An I/O error occurred while sending to the backend. errorCode: 0, sqlState: 08006 at ... [other stacktrace lines omitted]

Wednesday, June 10, 2015

Script for Jenkins 'Total build status' job

Script for Jenkins 'Total build status' job which displays all failed jobs with last changes, committers and links to job's console. It will fail if at least one other Jenkins job is failed.

Usage: 
- Create Jenkins Freestyle job
- Add this script as "Execute system Groovy script" build step 


Tuesday, May 26, 2015

Docker: check the memory used by docker containers at specific host


for line in `docker ps | awk '{print $1}' | grep -v CONTAINER`; do echo "" && docker ps | grep $line | awk '{printf $NF" "}' && echo -e "\nmemory usage: $(( `cat /sys/fs/cgroup/memory/docker/$line*/memory.usage_in_bytes` / 1024 / 1024 ))MB" && echo max memory usage: $(( `cat /sys/fs/cgroup/memory/docker/$line*/memory.max_usage_in_bytes` / 1024 / 1024 ))MB ; done

Tuesday, April 28, 2015

Oneliner for displaying apt packages which could be updated



function a { read input;dpkg -l ${input} | grep " ${input} " | awk '{$1=$2=$3=$4="";print $0}' | sed 's/^ *//';unset input;};{ apt-get --just-print upgrade 2>&1 | perl -ne 'if (/Inst\s([\w,\-,\d,\.,~,:,\+]+)\s\[([\w,\-,\d,\.,~,:,\+]+)\]\s\(([\w,\-,\d,\.,~,:,\+]+)\)? /i) {print "$1 (\e[1;34m$2\e[0m -> \e[1;32m$3\e[0m)\n"}';} | while read -r line; do echo -en "$line $(echo $line | awk '{print $1}' | a )\n"; done;





Example output (colored):

policykit-1 (0.105-4ubuntu2 -> 0.105-4ubuntu2.14.04.1) framework for managing administrative policies and privileges
python-urllib3 (1.7.1-1build1 -> 1.7.1-1ubuntu3) HTTP library with thread-safe connection pooling for Python
python-requests (2.2.1-1ubuntu0.1 -> 2.2.1-1ubuntu0.2) elegant and simple HTTP library for Python, built for human beings
software-properties-common (0.92.37.2 -> 0.92.37.3) manage the repositories that you install software from (common)
python3-software-properties (0.92.37.2 -> 0.92.37.3) manage the repositories that you install software from
cloud-guest-utils (0.27-0ubuntu9 -> 0.27-0ubuntu9.1) cloud guest utilities
cloud-init (0.7.5-0ubuntu1.3 -> 0.7.5-0ubuntu1.5) Init scripts for cloud instances
grub-legacy-ec2 (0.7.5-0ubuntu1.3 -> 0.7.5-0ubuntu1.5) Handles update-grub for ec2 instances
icinga2-doc (2.3.4-2~ppa1~trusty1 -> 2.3.4-3~ppa1~trusty1) host and network monitoring system - documentation

Monday, April 27, 2015

Useful nmap commands

Nmap scan of opened ports (including both udp and tcp ports):

sudo nmap --min-parallelism 100 -sT -sU -Pn localhost