Mostrando entradas con la etiqueta centos. Mostrar todas las entradas
Mostrando entradas con la etiqueta centos. Mostrar todas las entradas

lunes, 15 de mayo de 2017

We all understand free command... don't we???

I know I should be writing the next entry of "Understanding SOMAXCONN parameter Part I" (and I am) but instead I decided to do a little detour first. I faced this question about free a few days ago and noticed odd/different answers for it (including my own xD) so decided to go for a deep dive and found some interesting facts worth sharing.

Free


Free Linux command is one of those tools you will be definitely get exposed to as a sysadmin, and understanding its output is extremely important. A bad interpretation of free output could cause unnecessary panic and you don't want to panic for no reason :D (oh wait... maybe you do, I certainly don't ).

Lets have a look at a simple free output:

juan@test:~$ free
             total       used       free     shared    buffers     cached
Mem:       3007572     584872    2422700      14780      55808     291704
-/+ buffers/cache:     237360    2770212
Swap:            0          0          0
juan@test:~$

In the line starting with "Mem:" we get to see the total available physical memory on the system 3007572 KBytes (3GBytes) and then how it is distributed:
  • used: memory being occupied
  • free: memory that is available in the system
  • shared: amount of memory that is shared between processes
  • buffers: memory used for kernel buffers involving IO (disk, network, etc)
  • cached: memory used for data caching
Then we have a line starting with "-/+ buffers/cache:" which looks kind of cryptic, doesn't it?... it turns out to be a kind of summary of the previous line:
  • There are 237360 KBytes being used, this number is the result of the following math "used - (buffers + cached)". This is the amount of memory the system is actually relying on.
  • There are 2770212 KBytes that could be utilized, this is the result of "free + buffers + cached". What this number actually means is that the kernel should be able to reclaim buffer and cached memory if necessary.
So we have a better understanding of free output now, but lets get more details of where these numbers come from.

The power of the force source


Free binary is shipped as part of procps-ng package in Linux and we can inspect its latest code here. What free actually does for you is summarize memory utilization details coming from /proc/meminfo in a more "human way". So lets dissect this...
  • The main source code can be read here free.c, if you have a look at the lines between 359-377, you can see that there are 6 variables kb_main_total, kb_main_used, kb_main_free, kb_main_shared, kb_main_buffers and kb_main_cached and they are actually the ones containing the values that will show up in free's output. However if you look for these variables on the code you won't find them, they are extern variables included from "proc/sysinfo.h" which means the variables are being initialized somewhere else, particularly in "proc/sysinfo.c". The variables are actually being populated by meminfo() function being called in line 355.
  meminfo();
  /* Translation Hint: You can use 9 character words in
   * the header, and the words need to be right align to
   * beginning of a number. */
  if (flags & FREE_WIDE) {
   printf(_("              total        used        free      shared     buffers       cache   available"));
  } else {
   printf(_("              total        used        free      shared  buff/cache   available"));
  }
  printf("\n");
  printf("%-7s", _("Mem:"));
  printf(" %11s", scale_size(kb_main_total, flags, args));
  printf(" %11s", scale_size(kb_main_used, flags, args));
  printf(" %11s", scale_size(kb_main_free, flags, args));
  printf(" %11s", scale_size(kb_main_shared, flags, args));
  if (flags & FREE_WIDE) {
   printf(" %11s", scale_size(kb_main_buffers, flags, args));
   printf(" %11s", scale_size(kb_main_cached, flags, args));
  } else {
   printf(" %11s", scale_size(kb_main_buffers+kb_main_cached, flags, args));
  }
  printf(" %11s", scale_size(kb_main_available, flags, args));
  printf("\n");

  • Now checking the "proc/sysinfo.c" code where meminfo() function exists, we see:
    • kb_main_buffer is populated with the content of Buffers value from /proc/meminfo (line 691), this is memory used by the kernel to temporarily hold data being sent/received by the system (network IO, disk IO, etc).
    • kb_main_cached is populated with the result of "kb_page_cache + kb_slab_reclaimable" (line 762). These two values come from Cached and SReclaimable respectively (in /proc/meminfo). So the cached value presented by free contains the memory used by the page cache and the reclaimable slab memory (slab memory caches dentry and inodes structures to speed up some fs operations).
  {"Bounce",       &kb_bounce},
  {"Buffers",      &kb_main_buffers}, // important
  {"Cached",       &kb_page_cache},  // important
  {"CommitLimit",  &kb_commit_limit},
  ...
  kb_main_cached = kb_page_cache + kb_slab_reclaimable;
  kb_swap_used = kb_swap_total - kb_swap_free;

It is clear now that free relies completely on the details exposed by the kernel through /proc/meminfo, and the math involved in getting the values isn't really rocket science after all. But here it comes the interesting part...

To have in mind


It looks like the information provided by free can be slightly different between different Linux flavors (because they carry different free versions :D) so I thought it might be worth pointing that out here as well. This could lead to some inconsistencies when some other tools rely on the output of free.

Free version shipped with Ubuntu 14.04 (3.3.9), doesn't even include Slabs under the cached memory, you can see here how kb_main_cached value is just populated with Cached value from /proc/meminfo.

juan@test:~$ free -V
free from procps-ng 3.3.9
juan@test:~$ free
             total       used       free     shared    buffers     cached
Mem:       3007572     530948    2476624      14776      53080     244740
-/+ buffers/cache:     233128    2774444
Swap:            0          0          0
juan@test:~$ grep "^Cached\|^SReclaimable\|^Slab" /proc/meminfo
Cached:           244740 kB
Slab:              32308 kB
SReclaimable:      17572 kB
juan@test:~$

Note: cached = Cached

Free version shipped with CentOS 7 (3.3.10), defines kb_main_cached as "kb_page_cache + kb_slab" (706) this seems a minor thing but not all the slab memory is reclaimable therefore part of this cached content is not really available in case of memory pressure.

[juan@server ~]$ free -V
free from procps-ng 3.3.10
[juan@server ~]$ free -w
              total        used        free      shared     buffers       cache   available
Mem:        1016860       74212      818380        6688         948      123320      810600
Swap:             0           0           0
[juan@server ~]$ grep "^Cached\|^SRecl\|^Slab" /proc/meminfo
Cached:            88876 kB
Slab:              34444 kB
SReclaimable:      13928 kB
[juan@server ~]$

Note: cache = Cached + Slab

Interesting, isn't it? That's the power of open source after all right? Having the chance to really understand what certain piece of software is doing for you and how is doing it!

That's all about free, hope it helps (it helped me at least :P).

sábado, 6 de mayo de 2017

Understanding SOMAXCONN parameter Part I

This post is kind of a fork of something I worked on the previous week and tries to shed some light on what somaxconn parameter means in the context of Linux Network stack. I've tried to prepare something similar to what I did in TCP Keep Alive - how it works, with examples and some traffic captures so I guess this article will be a bit longer that [I|you]'d like xD, but useful (hopefully).

somaxconn


By definition this value is the maximum number of established connections a socket may have queuing waiting to be accepted by the process holding the socket. This is completely different from tcp_syn_max_backlog since this value is for incomplete connection (connections that haven't been ACKed yet).

To give more context to that statement is necessary to understand a bit more about sockets. When a socket is created, before being able to receive connections two things must happen:
  1. It has to be bind() to a local address.
  2. It has to bet set to listen() for connections.
The listen() syscall is key here, it receives 2 parameters:
Long time ago (before kernel 2.4.25) somaxconn value used to be hardcoded in the kernel, nowadays you can read and update using sysctl as with the rest of the network stack parameters. In most systems the default value is 128, like in my CentOS VM:

[root@server juan]# sysctl -a --pattern net.core.somaxconn
net.core.somaxconn = 128
[root@server juan]#

Test scenario


In order to test and confirm the behavior of somaxconn I have the following setup:
  • Client VM (IP 192.168.0.17) running a C binary (source here) that will start 25 TCP connections to the Server VM on port 8080, in each connection it will send 4 bytes "juan". The binary opens 1 connection per second, and after having opened the 25 connections just waits for 2 minutes to finish.
  • Server VM (IP 192.168.0.26) running ncat (nc, netcat, whatever you like to call it) listening on port 8080, and the maximum open files has been set to 9 (ulimit -n 9) to achieve a condition where we can see established connections being queued easier.
A few more details you should keep in mind:
  • The version of nc shipped with CentOS sets a backlog of 10 on the listen() syscall (using strace):
[juan@server ~]$ ulimit -n 9
[juan@server ~]$ strace nc -lk 8080
execve("/usr/bin/nc", ["nc", "-lk", "8080"], [/* 24 vars */]) = 0
brk(0)                                  = 0x1dd7000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fe7075e9000
...
bind(3, {sa_family=AF_INET6, sin6_port=htons(8080), inet_pton(AF_INET6, "::", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 128) = 0
listen(3, 10)                           = 0
fcntl(3, F_GETFL)
...
  • So we have a backlog of 10 set in the listen syscall and a limit of 9 open files. I will use this value as somaxconn since the meaning is the same.
  • After nc was just started we can see it has already used 5 FD in use (STDIN, STDOUT, STDERR, and two more for the listening sockets on IPv4 and IPv6)
[root@server juan]# lsof -p 14136|grep "CHR\|IPv"
nc      14136 juan    0u   CHR     136,0      0t0        3 /dev/pts/0
nc      14136 juan    1u   CHR     136,0      0t0        3 /dev/pts/0
nc      14136 juan    2u   CHR     136,0      0t0        3 /dev/pts/0
nc      14136 juan    3u  IPv6 488270957      0t0      TCP *:webcache (LISTEN)
nc      14136 juan    4u  IPv4 488270958      0t0      TCP *:webcache (LISTEN)
[root@server juan]#

With all these details in mind and some basic math (2+2=4 :D) we can expect the following behavior:
  • Since 5 out of the 9 available file descriptors are already in use, nc should not be able to receive more than 4 new connections on port 8080. Each new connection will create a new socket that has to be accessible in user space through a file descriptor (have a look at accept() syscall), which can't be created if the max open files limit has been reached.
  • The C binary will issue 25 connections:
    • The first 4 will be answered just fine by nc and the 4 bytes will be read from the socket and printed to STDOUT.
    • The next 10 connections will be indeed accepted by the Network layer of the Server VM, and even the 4 bytes will be received, however nc won't be able to accept the socket and read the 4 bytes. These are the established connections being queued!!!
    •  The remaining 11 connections will look as ESTABLISHED from the client as they have completed the TCP handshake however the 4 bytes won't be ACKnowledged by Network Stack because the queue is full (somaxconn limit has been reached). From the server the connections won't even exist.

Binary execution:

juan@test:~/TCP_connections$ gcc -o tcp_connections tcp_connections.c
juan@test:~/TCP_connections$ ./tcp_connections
Socket opened FD=3, connection number 0
Socket opened FD=4, connection number 1
Socket opened FD=5, connection number 2
Socket opened FD=6, connection number 3
Socket opened FD=7, connection number 4
Socket opened FD=8, connection number 5
Socket opened FD=9, connection number 6
Socket opened FD=10, connection number 7
Socket opened FD=11, connection number 8
Socket opened FD=12, connection number 9
Socket opened FD=13, connection number 10
Socket opened FD=14, connection number 11
Socket opened FD=15, connection number 12
Socket opened FD=16, connection number 13
Socket opened FD=17, connection number 14
Socket opened FD=18, connection number 15
Socket opened FD=19, connection number 16
Socket opened FD=20, connection number 17
Socket opened FD=21, connection number 18
Socket opened FD=22, connection number 19
Socket opened FD=23, connection number 20
Socket opened FD=24, connection number 21
Socket opened FD=25, connection number 22
Socket opened FD=26, connection number 23
Socket opened FD=27, connection number 24
juan@test:~/TCP_connections$

Nc execution on the server


Note: I introduced the new line after the 4th connections just to split them from the rest:
[juan@server ~]$ nc -lvk -p 8080
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Listening on :::8080
Ncat: Listening on 0.0.0.0:8080
Ncat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49026.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49027.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49028.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49029.
juan

Ncat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49030.
Ncat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49031.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49032.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49033.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49034.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49035.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49036.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49037.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49038.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49039.
juanNcat: Connection from 192.168.0.17.
Ncat: Connection from 192.168.0.17:49040.
juanjuan 

Results


To have a  look at what was happening during the test I measured 3 different locations:
  1. TCP connections on the client using ss.
  2. TCP connections on the server using ss.
  3. Captured network traffic on the server using tcpdump.
These 3 locations provided enough information to understand what is going on and to even find something interesting :D.

TCP connections on the client:


I've chopped off a couple of lines for the sake of space and time.  The samples were taken every 2 seconds and only TCP connections to port 8080 were considered:

root@test:/home/juan# for i in {1..100}; do ss -ntpe|grep "State\|8080"; echo ---; sleep 2; done
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
ESTAB      0      0              192.168.0.17:49026         192.168.0.26:8080   users:(("tcp_connections",3342,3)) uid:1000 ino:23326 sk:ffff8800b4914fc0
---
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
ESTAB      0      0              192.168.0.17:49027         192.168.0.26:8080   users:(("tcp_connections",3342,4)) uid:1000 ino:23350 sk:ffff8800b4914140
ESTAB      0      0              192.168.0.17:49028         192.168.0.26:8080   users:(("tcp_connections",3342,5)) uid:1000 ino:23368 sk:ffff8800b4910e80
ESTAB      0      0              192.168.0.17:49026         192.168.0.26:8080   users:(("tcp_connections",3342,3)) uid:1000 ino:23326 sk:ffff8800b4914fc0
---
...
---
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
ESTAB      0      0              192.168.0.17:49031         192.168.0.26:8080   users:(("tcp_connections",3342,8)) uid:1000 ino:23408 sk:ffff8800b4911d00
ESTAB      0      0              192.168.0.17:49027         192.168.0.26:8080   users:(("tcp_connections",3342,4)) uid:1000 ino:23350 sk:ffff8800b4914140
ESTAB      0      0              192.168.0.17:49029         192.168.0.26:8080   users:(("tcp_connections",3342,6)) uid:1000 ino:23372 sk:ffff8800b4910740
ESTAB      0      0              192.168.0.17:49030         192.168.0.26:8080   users:(("tcp_connections",3342,7)) uid:1000 ino:23390 sk:ffff8800b49115c0
ESTAB      0      0              192.168.0.17:49028         192.168.0.26:8080   users:(("tcp_connections",3342,5)) uid:1000 ino:23368 sk:ffff8800b4910e80
ESTAB      0      0              192.168.0.17:49026         192.168.0.26:8080   users:(("tcp_connections",3342,3)) uid:1000 ino:23326 sk:ffff8800b4914fc0
ESTAB      0      0              192.168.0.17:49033         192.168.0.26:8080   users:(("tcp_connections",3342,10)) uid:1000 ino:23445 sk:ffff8800b4912b80
ESTAB      0      0              192.168.0.17:49034         192.168.0.26:8080   users:(("tcp_connections",3342,11)) uid:1000 ino:23447 sk:ffff8800b49132c0
ESTAB      0      0              192.168.0.17:49032         192.168.0.26:8080   users:(("tcp_connections",3342,9)) uid:1000 ino:23426 sk:ffff8800b4912440
---
...


After a few seconds, we can see a few ESTABLISHED tcp connections using source ports from 49026 to 49034, 9 connections to be more precise, and they all show 0 bytes in Send-Q which suggests the 4 bytes sent by the application were actually acknowledged by the server.

...
---
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
ESTAB      0      0              192.168.0.17:49031         192.168.0.26:8080   users:(("tcp_connections",3342,8)) uid:1000 ino:23408 sk:ffff8800b4911d00
ESTAB      0      0              192.168.0.17:49027         192.168.0.26:8080   users:(("tcp_connections",3342,4)) uid:1000 ino:23350 sk:ffff8800b4914140
ESTAB      0      0              192.168.0.17:49029         192.168.0.26:8080   users:(("tcp_connections",3342,6)) uid:1000 ino:23372 sk:ffff8800b4910740
ESTAB      0      0              192.168.0.17:49030         192.168.0.26:8080   users:(("tcp_connections",3342,7)) uid:1000 ino:23390 sk:ffff8800b49115c0
ESTAB      0      0              192.168.0.17:49028         192.168.0.26:8080   users:(("tcp_connections",3342,5)) uid:1000 ino:23368 sk:ffff8800b4910e80
ESTAB      0      0              192.168.0.17:49037         192.168.0.26:8080   users:(("tcp_connections",3342,14)) uid:1000 ino:23502 sk:ffff8800b4917400
ESTAB      0      0              192.168.0.17:49035         192.168.0.26:8080   users:(("tcp_connections",3342,12)) uid:1000 ino:23465 sk:ffff8800b4913a00
ESTAB      0      0              192.168.0.17:49036         192.168.0.26:8080   users:(("tcp_connections",3342,13)) uid:1000 ino:23484 sk:ffff8800b4915e40
ESTAB      0      0              192.168.0.17:49026         192.168.0.26:8080   users:(("tcp_connections",3342,3)) uid:1000 ino:23326 sk:ffff8800b4914fc0
ESTAB      0      0              192.168.0.17:49038         192.168.0.26:8080   users:(("tcp_connections",3342,15)) uid:1000 ino:23520 sk:ffff8800b4916580
ESTAB      0      0              192.168.0.17:49033         192.168.0.26:8080   users:(("tcp_connections",3342,10)) uid:1000 ino:23445 sk:ffff8800b4912b80
ESTAB      0      0              192.168.0.17:49034         192.168.0.26:8080   users:(("tcp_connections",3342,11)) uid:1000 ino:23447 sk:ffff8800b49132c0
ESTAB      0      0              192.168.0.17:49040         192.168.0.26:8080   users:(("tcp_connections",3342,17)) uid:1000 ino:23557 sk:ffff8800b4914880
ESTAB      0      0              192.168.0.17:49039         192.168.0.26:8080   users:(("tcp_connections",3342,16)) uid:1000 ino:23539 sk:ffff8800b4915700
ESTAB      0      0              192.168.0.17:49032         192.168.0.26:8080   users:(("tcp_connections",3342,9)) uid:1000 ino:23426 sk:ffff8800b4912440
---
...

A few more seconds later we see even more TCP connections in ESTABLISHED state, including src ports from 49035 to 49040. So we have a total of 15 ESTABLISHED connections with 0 bytes waiting to be ACKnowledged by the server. Remember we were expecting 14 (4 + 10), but instead we got 15 (4 + 11), odd right? I'll get back to this later...

...
---
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
ESTAB      0      0              192.168.0.17:49031         192.168.0.26:8080   users:(("tcp_connections",3342,8)) uid:1000 ino:23408 sk:ffff8800b4911d00
ESTAB      0      0              192.168.0.17:49027         192.168.0.26:8080   users:(("tcp_connections",3342,4)) uid:1000 ino:23350 sk:ffff8800b4914140
ESTAB      0      0              192.168.0.17:49029         192.168.0.26:8080   users:(("tcp_connections",3342,6)) uid:1000 ino:23372 sk:ffff8800b4910740
ESTAB      0      0              192.168.0.17:49030         192.168.0.26:8080   users:(("tcp_connections",3342,7)) uid:1000 ino:23390 sk:ffff8800b49115c0
ESTAB      0      0              192.168.0.17:49028         192.168.0.26:8080   users:(("tcp_connections",3342,5)) uid:1000 ino:23368 sk:ffff8800b4910e80
ESTAB      0      0              192.168.0.17:49037         192.168.0.26:8080   users:(("tcp_connections",3342,14)) uid:1000 ino:23502 sk:ffff8800b4917400
ESTAB      0      0              192.168.0.17:49035         192.168.0.26:8080   users:(("tcp_connections",3342,12)) uid:1000 ino:23465 sk:ffff8800b4913a00
ESTAB      0      0              192.168.0.17:49036         192.168.0.26:8080   users:(("tcp_connections",3342,13)) uid:1000 ino:23484 sk:ffff8800b4915e40
ESTAB      0      0              192.168.0.17:49026         192.168.0.26:8080   users:(("tcp_connections",3342,3)) uid:1000 ino:23326 sk:ffff8800b4914fc0
ESTAB      0      0              192.168.0.17:49038         192.168.0.26:8080   users:(("tcp_connections",3342,15)) uid:1000 ino:23520 sk:ffff8800b4916580
ESTAB      0      0              192.168.0.17:49033         192.168.0.26:8080   users:(("tcp_connections",3342,10)) uid:1000 ino:23445 sk:ffff8800b4912b80
ESTAB      0      0              192.168.0.17:49034         192.168.0.26:8080   users:(("tcp_connections",3342,11)) uid:1000 ino:23447 sk:ffff8800b49132c0
ESTAB      0      0              192.168.0.17:49040         192.168.0.26:8080   users:(("tcp_connections",3342,17)) uid:1000 ino:23557 sk:ffff8800b4914880
ESTAB      0      4              192.168.0.17:49041         192.168.0.26:8080   timer:(on,1.208ms,3) users:(("tcp_connections",3342,18)) uid:1000 ino:23575 sk:ffff8800b4916cc0
ESTAB      0      0              192.168.0.17:49039         192.168.0.26:8080   users:(("tcp_connections",3342,16)) uid:1000 ino:23539 sk:ffff8800b4915700
ESTAB      0      0              192.168.0.17:49032         192.168.0.26:8080   users:(("tcp_connections",3342,9)) uid:1000 ino:23426 sk:ffff8800b4912440
ESTAB      0      4              192.168.0.17:49042         192.168.0.26:8080   timer:(on,608ms,2) users:(("tcp_connections",3342,19)) uid:1000 ino:23593 sk:ffff8800b4910000
---
...

This new sample shows 2 lines that are different than the rest, the ones using src ports 49041 and 49042.  These were the first 2 connections that couldn't fit in the somaxconn queue!!! And even more, you can see how the 4 bytes that were sent from the client haven't been acknowledged by the server and therefore they show up in the Send-Q column. Note how the connection looks as ESTABLISHED for the client though, this is because the 3-way handshake was completed from the client point of view.

...
---
State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
ESTAB      0      0              192.168.0.17:49031         192.168.0.26:8080   users:(("tcp_connections",3342,8)) uid:1000 ino:23408 sk:ffff8800b4911d00
ESTAB      0      0              192.168.0.17:49027         192.168.0.26:8080   users:(("tcp_connections",3342,4)) uid:1000 ino:23350 sk:ffff8800b4914140
ESTAB      0      0              192.168.0.17:49029         192.168.0.26:8080   users:(("tcp_connections",3342,6)) uid:1000 ino:23372 sk:ffff8800b4910740
ESTAB      0      4              192.168.0.17:49047         192.168.0.26:8080   timer:(on,460ms,4) users:(("tcp_connections",3342,24)) uid:1000 ino:23632 sk:ffff8800a6efba00
ESTAB      0      4              192.168.0.17:49050         192.168.0.26:8080   timer:(on,132ms,3) users:(("tcp_connections",3342,27)) uid:1000 ino:23635 sk:ffff8800a6efc140
ESTAB      0      0              192.168.0.17:49030         192.168.0.26:8080   users:(("tcp_connections",3342,7)) uid:1000 ino:23390 sk:ffff8800b49115c0
ESTAB      0      0              192.168.0.17:49028         192.168.0.26:8080   users:(("tcp_connections",3342,5)) uid:1000 ino:23368 sk:ffff8800b4910e80
ESTAB      0      4              192.168.0.17:49045         192.168.0.26:8080   timer:(on,4.740ms,5) users:(("tcp_connections",3342,22)) uid:1000 ino:23630 sk:ffff8800a6efcfc0
ESTAB      0      4              192.168.0.17:49049         192.168.0.26:8080   timer:(on,2.336ms,4) users:(("tcp_connections",3342,26)) uid:1000 ino:23634 sk:ffff8800a6efc880
ESTAB      0      0              192.168.0.17:49037         192.168.0.26:8080   users:(("tcp_connections",3342,14)) uid:1000 ino:23502 sk:ffff8800b4917400
ESTAB      0      0              192.168.0.17:49035         192.168.0.26:8080   users:(("tcp_connections",3342,12)) uid:1000 ino:23465 sk:ffff8800b4913a00
ESTAB      0      4              192.168.0.17:49043         192.168.0.26:8080   timer:(on,2.740ms,5) users:(("tcp_connections",3342,20)) uid:1000 ino:23611 sk:ffff8800a6efde40
ESTAB      0      0              192.168.0.17:49036         192.168.0.26:8080   users:(("tcp_connections",3342,13)) uid:1000 ino:23484 sk:ffff8800b4915e40
ESTAB      0      4              192.168.0.17:49044         192.168.0.26:8080   timer:(on,3.732ms,5) users:(("tcp_connections",3342,21)) uid:1000 ino:23616 sk:ffff8800a6efd700
ESTAB      0      4              192.168.0.17:49046         192.168.0.26:8080   timer:(on,5.740ms,5) users:(("tcp_connections",3342,23)) uid:1000 ino:23631 sk:ffff8800a6eff400
ESTAB      0      0              192.168.0.17:49026         192.168.0.26:8080   users:(("tcp_connections",3342,3)) uid:1000 ino:23326 sk:ffff8800b4914fc0
ESTAB      0      4              192.168.0.17:49048         192.168.0.26:8080   timer:(on,1.336ms,4) users:(("tcp_connections",3342,25)) uid:1000 ino:23633 sk:ffff8800a6efecc0
ESTAB      0      0              192.168.0.17:49038         192.168.0.26:8080   users:(("tcp_connections",3342,15)) uid:1000 ino:23520 sk:ffff8800b4916580
ESTAB      0      0              192.168.0.17:49033         192.168.0.26:8080   users:(("tcp_connections",3342,10)) uid:1000 ino:23445 sk:ffff8800b4912b80
ESTAB      0      0              192.168.0.17:49034         192.168.0.26:8080   users:(("tcp_connections",3342,11)) uid:1000 ino:23447 sk:ffff8800b49132c0
ESTAB      0      0              192.168.0.17:49040         192.168.0.26:8080   users:(("tcp_connections",3342,17)) uid:1000 ino:23557 sk:ffff8800b4914880
ESTAB      0      4              192.168.0.17:49041         192.168.0.26:8080   timer:(on,732ms,5) users:(("tcp_connections",3342,18)) uid:1000 ino:23575 sk:ffff8800b4916cc0
ESTAB      0      0              192.168.0.17:49039         192.168.0.26:8080   users:(("tcp_connections",3342,16)) uid:1000 ino:23539 sk:ffff8800b4915700
ESTAB      0      0              192.168.0.17:49032         192.168.0.26:8080   users:(("tcp_connections",3342,9)) uid:1000 ino:23426 sk:ffff8800b4912440
ESTAB      0      4              192.168.0.17:49042         192.168.0.26:8080   timer:(on,1.732ms,5) users:(("tcp_connections",3342,19)) uid:1000 ino:23593 sk:ffff8800b4910000
---
^C
root@test:/home/juan#

The last sample shows the remaining connections from ports 49043 to 49050 all of them in the same condition, 4 bytes not being acknowledged.

From the client side samples we can confirm we got 15 complete TCP connections (src ports from 49026 to 49040) and 10 that even though showed up as ESTABLISHED weren't able to get acknowledged the 4 bytes sent by the client (src ports from 49041 to 49050).

TCP connections on the server


Lets have a look now to the samples taken on the server and see if they match with what we found on the client.

[root@server juan]# for i in {1..100}; do ss -tpn|grep "State\|8080"; echo "---"; sleep 2;done
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49026               users:(("nc",pid=13705,fd=5))
---
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49026               users:(("nc",pid=13705,fd=5))
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49028               users:(("nc",pid=13705,fd=7))
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49027               users:(("nc",pid=13705,fd=6))
---
...

The first samples look ok, nothing fancy there, we can see the first 3 TCP connections using src ports from 49026 to 49028.

...
---
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49026               users:(("nc",pid=13705,fd=5))
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49031
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49033
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49029               users:(("nc",pid=13705,fd=8))
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49028               users:(("nc",pid=13705,fd=7))
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49032
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49030
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49027               users:(("nc",pid=13705,fd=6))
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49034
---
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49036
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49026               users:(("nc",pid=13705,fd=5))
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49035
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49031
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49033
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49029               users:(("nc",pid=13705,fd=8))
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49028               users:(("nc",pid=13705,fd=7))
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49032
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49030
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49027               users:(("nc",pid=13705,fd=6))
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49034
---
...

And here it got interesting! We have now 4 connections (src ports between 49026 and 49029) which besides being ESTABLISHED have a FD associated to our nc process. However we have another 7 ESTABLISHED connections (src ports from 49030 to 49036) with no FD associated (and no process either :D)!!! These 7 connections are the ones waiting on the queue for nc to issue the accept() call. nc is actually issuing the accept calls, but the calls are failing due to nc having reached the maximum open files (because of the "ulimit -n 9").
 
...
---
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49036
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49026               users:(("nc",pid=13705,fd=5))
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49038
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49035
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49031
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49033
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49029               users:(("nc",pid=13705,fd=8))
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49028               users:(("nc",pid=13705,fd=7))
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49032
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49039
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49030
ESTAB      0      0      192.168.0.26:8080               192.168.0.17:49027               users:(("nc",pid=13705,fd=6))
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49037
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49040
ESTAB      4      0      192.168.0.26:8080               192.168.0.17:49034
---
^C
[root@server juan]#

After the binary issued the 25 connections, on the server we can see 4 ESTABLISHED connections (src ports 49026 to 49029) with corresponding FDs and 11 ESTABLISHED connections (src ports 49030 to 49040) with no FD. So... where are the remaining 10 connections that show up as ESTABLISHED on the client??? Well, these 10 connections didn't reach ESTABLISHED state on the server :D as per the documentation of listen():

The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow.  If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds.

Considering we are using TCP , the use case matches exactly with the underlined part.

To be continued...

miércoles, 12 de noviembre de 2014

Gluster: usando quotas

Una de las funcionalidades de gluster que me resultó de gran utilidad en estos días dada la escasez de espacio son las quotas. Básicamente las quotas nos permiten asignar límites de uso de espacio a directorios dentro de los volúmenes de gluster.

Supongamos que tenemos 2 aplicaciones aplicacion1 y aplicacion2 que tienen acceso a directorios homónimos dentro de un volumen gluster replicado llamado contenedor. A continuación vemos el volumen en cuestión:


[root@gluster-server-2 ~]# gluster vol info

Volume Name: contenedor
Type: Replicate
Volume ID: 3933514f-f5a4-4260-9d40-964c6cc44b59
Status: Started
Number of Bricks: 1 x 2 = 2
Transport-type: tcp
Bricks:
Brick1: 172.16.47.15:/mnt/1
Brick2: 172.16.47.16:/mnt/1
[root@gluster-server-2 ~]# 

El primer paso para utilizar quotas es habilitarlas en el volumen:

[root@gluster-server-2 ~]# gluster vol quota contenedor enable
Enabling quota has been successful
[root@gluster-server-2 ~]# 

Verificamos que la operación se haya concretado correctamente:

[root@gluster-server-2 ~]# gluster vol info
 
Volume Name: contenedor
Type: Replicate
Volume ID: 3933514f-f5a4-4260-9d40-964c6cc44b59
Status: Started
Number of Bricks: 1 x 2 = 2
Transport-type: tcp
Bricks:
Brick1: 172.16.47.15:/mnt/1
Brick2: 172.16.47.16:/mnt/1
Options Reconfigured:
features.quota: on
[root@gluster-server-2 ~]# 

Ahora que las quotas están habilitadas podemos configurarlas. En este caso cofiguramos una quota de 1GB para el directorio aplicacion1 y 512MB para el directorio aplicacion2:

[root@gluster-server-2 ~]# gluster vol quota contenedor limit-usage /aplicacion1 1024MB
limit set on /aplicacion1
[root@gluster-server-2 ~]# gluster vol quota contenedor limit-usage /aplicacion2 512MB
limit set on /aplicacion2
[root@gluster-server-2 ~]# 

Corroboramos que se hayan aplicado las quotas:

[root@gluster-server-2 ~]# gluster vol info
 
Volume Name: contenedor
Type: Replicate
Volume ID: 3933514f-f5a4-4260-9d40-964c6cc44b59
Status: Started
Number of Bricks: 1 x 2 = 2
Transport-type: tcp
Bricks:
Brick1: 172.16.47.15:/mnt/1
Brick2: 172.16.47.16:/mnt/1
Options Reconfigured:
features.limit-usage: /aplicacion1:1024MB,/aplicacion2:512MB
features.quota: on
[root@gluster-server-2 ~]# 

Ahora probamos las cuotas escribiendo en los directorios desde el cliente que monta el volumen. El volumen lo montamos en /mnt:

[root@gluster-client ~]# mount
/dev/vda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
172.16.47.15:/contenedor on /mnt type fuse.glusterfs (rw,default_permissions,allow_other,max_read=131072)
[root@gluster-client ~]# ls /mnt/
aplicacion1  aplicacion2
[root@gluster-client ~]# 

Primero escribimos en ambos directorios hasta poco antes del límite:

[root@gluster-client ~]# dd if=/dev/zero of=/mnt/aplicacion1/archivo_grande bs=1M count=1020
1020+0 records in
1020+0 records out
1069547520 bytes (1.1 GB) copied, 32.5717 s, 32.8 MB/s
[root@gluster-client ~]# dd if=/dev/zero of=/mnt/aplicacion2/archivo_grande bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 14.0041 s, 37.4 MB/s
[root@gluster-client ~]# 

Vemos cuánto escribimos en cada directorio:

[root@gluster-client ~]# du -sch /mnt/*
1020M /mnt/aplicacion1
500M /mnt/aplicacion2
1.5G total
[root@gluster-client ~]# 

De momento nos mantuvimos por debajo de los límites definidos en las cuotas, por eso no tuvimos problema alguno. Podemos verificar el estado de las cuotas fijadas de la siguiente manera:

[root@gluster-server-2 ~]# gluster vol quota contenedor list
path  limit_set     size
----------------------------------------------------------------------------------
/aplicacion1             1024MB             1020.0MB
/aplicacion2              512MB              500.0MB
[root@gluster-server-2 ~]# 

Ahora ponemos a prueba el control escribiendo mas allá de lo permitido con un nuevo archivo:

[root@gluster-client ~]# dd if=/dev/zero of=/mnt/aplicacion1/archivo_chico bs=1M count=100
dd: escribiendo «/mnt/aplicacion1/archivo_chico»: Se ha excedido la cuota de disco
6+0 records in
5+0 records out
5898240 bytes (5.9 MB) copied, 0.141132 s, 41.8 MB/s
[root@gluster-client ~]# dd if=/dev/zero of=/mnt/aplicacion2/archivo_chico bs=1M count=100
dd: escribiendo «/mnt/aplicacion2/archivo_chico»: Se ha excedido la cuota de disco
dd: cerrando el fichero de salida «/mnt/aplicacion2/archivo_chico»: Se ha excedido la cuota de disco
[root@gluster-client ~]# 

JA! No pudimos escribir demasiado, alcanzamos los límites fijados por las cuotas:

[root@gluster-client ~]# du -sch /mnt/*
1.1G /mnt/aplicacion1
513M /mnt/aplicacion2
1.6G total
[root@gluster-client ~]# 

también lo podemos ver desde gluster:

[root@gluster-server-2 ~]# gluster vol quota contenedor list
path  limit_set     size
----------------------------------------------------------------------------------
/aplicacion1             1024MB                1.0GB
/aplicacion2              512MB              512.5MB
[root@gluster-server-2 ~]# 

Una vez alcanzada la cuota ni siquiera se nos permite crear nuevos archivos o directorios:

[root@gluster-client ~]# touch /mnt/aplicacion1/a
touch: no se puede efectuar `touch' sobre «/mnt/aplicacion1/a»: Se ha excedido la cuota de disco
[root@gluster-client ~]# touch /mnt/aplicacion2/a
touch: no se puede efectuar `touch' sobre «/mnt/aplicacion2/a»: Se ha excedido la cuota de disco
[root@gluster-client ~]# mkdir /mnt/aplicacion1/b
mkdir: no se puede crear el directorio «/mnt/aplicacion1/b»: Se ha excedido la cuota de disco
[root@gluster-client ~]# mkdir /mnt/aplicacion2/b
mkdir: no se puede crear el directorio «/mnt/aplicacion2/b»: Se ha excedido la cuota de disco
[root@gluster-client ~]# 

Las cuotas pueden ser aplicadas a sub directorios también, y un punto a considerar es que se trata de cuotas duras, es decir que NO se pueden sobrepasar.

jueves, 13 de marzo de 2014

KeePassX, protegiendo mis claves, digale NO!!! a las claves en papel o en un txt

Cuántas claves recordás? Hoy por hoy todos tenemos almenos:

-Una cuenta de correo electrónico
-Una cuenta en Facebook
-Una cuenta en twitter
-Una cuenta de home banking
-Una tarjeta de débito/crédito
-Una cuenta en algún foro, diario, revista, etc
-etc...

Pero quién tiene una clave diferente para cada servicio? Me consta que muchas personas caen en el error de re-utilizar sus claves porque les resulta complicado/tedioso/aburrido/feo tener que recordar tantas, pero esto potencia mas aún el riesgo de por ejemplo sufrir un robo de identidad, fraude electrónico y una larga lista de problemas. Por ejemplo, es un clasico que una persona use en facebook la misma contraseña que utiliza en su correo electrónico. Otro clásico es no utilizar la misma clave, sino una con una leve diferencia por ejemplo: la clave del mail es pepeloco y la clave de facebook es pepeloco1. Esto si bien resuelve dos cosas, recordar la clave y que no sea la misma, es una solución demasiado ingénua y no vale la pena.

Si bien tener distintas claves para cada servicio no resuelve todo el problema (si las claves son sencillas, estamos en el horno igual), puede disminuir de manera considerable el impacto del descubrimiento de una de ellas, es decir: una clave revelada un servicio afectado y ya no una clave revelada muchos servicios afectados. Poniendolo en criollo, si obtienen tu clave del mail solo van a poder mandar mails y ya no hacerse pasar por vos en Facebook y Twitter.

También es cierto que existen soluciones como single sign-on que atacan en cierta forma esta problemática, pero en este caso tomaremos una dirección distinta.

Hay varios programas que permiten almacenar de manera segura nuestras claves y en este post en particular hablaremos de KeePassX. Keepassx nos va a permitir almacenar y ordenar nuestras claves de manera sencilla y prolija.

keepassx


Pero donde está la parte mágica de Keepassx? no hay magia, keepassx protegerá nuestras claves con... una clave. En esencia, ahora debemos elegir una única clave muy fuerte para que proteja a las demás claves que podrían ser incluso generadas aleatoriamente, ya que no precisamos recordarlas!!! Esta clave que debemos elegir debe ser realmente segura y no deberíamos anda gritándola ni escribiéndola en ningún lado, ya que será utilizada por keepassx para ocultar las demas. En criollo Keepassx es una caja fuerte donde guardamos las llaves de cada una de nuestras propiedades, si le entregamos a un ladron la caja fuerte junto con la combinación nos podemos despedir de las propiedades, pero si el ladrón obtiene solo la caja fuerte no puede hacer demasiado (en este caso, con una clave segura el ladrón está perdido).

La instalación va a depender de si se trata de Windows o Linux (mas sencilla ya que está en los repos de cualquier distro apt-get install keepassx o yum install keepassx). Para el caso de windows pueden bajar el instalador de AQUI, funciona en Windows 2000, XP, Vista, 7.

Una vez instalado lo ejecutan y buscan la opción "Nueva Base de Datos", les pedirá que ingresen una contraseña y que la repitan. Aquí radica gran parte de la fortaleza del esquema, esta clave debe ser larga, contener numeros, símbolos, todo lo que puedan recordar. A partir de aquí es sencillo agregar y quitar registros, armar grupos, etc (imagenes oficiales de keepassx). Keepassx encripta la base de datos con las claves utilizando AES256 lo que significa que será computacionalmente imposible adivinar una clave fuerte (al menos al día de la fecha, claro).

Por cada entrada registrada en KeePassX podemos registrar la siguiente información:

entrada keepassx

Grupo al que pertenece, es una forma sencilla de ubicar las entradas.
Título con el que se podrá encontrarla en keepassx
Usuario, esto podria ser una dirección de correo electrónico en el caso de Facebook
URL, la dirección del sitio donde este usuario y clave son válidos
Clave/Password, la clave en si misma. Como verán está el botón Gen que permitirá generar una clave aleatoria.
Comentario, un cuadro de texto donde podremos escribir información adicional si es necesario
Adjunto/Attachment, es aqui donde se puede adjuntar un archivo a la entrada. 

Otra particularidad de keepassx es que nos permite incluir archivos en esta pequeña base de datos, interesante para por ejemplo casos donde tengamos alguna información importante en una imagen jpg o un keyfile de algún otro sistema encriptado.

Una vez cargada una entrada a keepassx, basta con ubicarla y presionar botón derecho para copiar el usuario y luego de la misma manera se puede copiar la clave.

claves keepassx

De esta forma conseguimos, sacrificando un poco la comodidad, una manera sencilla de administrar nuestros passwords de forma segura.

Algunas recomendaciones para el uso de KeePaasX


  • Mantenerlo actualizado, es decir, si uno actualiza su password de facebook debe actualizarlo de igual manera en KeePassX, porque sino el password sería obsoleto. 
  • Tener al menos dos copias del archivo de claves de KeePassx repartidas en lugares diferentes (una en la computadora y la otra en un pendrive por ejemplo). Dada la importancia del archivo, si le pasara algo a la computadora y no pudiesemos recuperarlo estaríamos frente a un gran problema.
  • El password que protege el archivo debe ser considerablemente fuerte, en lo posible con mas de 10 caracteres, mezclando letras y números; y si es posible símbolos también. Pero ante todo el password debe ser fácil de recordar, dado que si se lo olvidan de nuevo estamos en problemas.

lunes, 26 de septiembre de 2011

Ver actualizaciones Ubuntu + CentOS + OpenSuSE

Justamente en la entrada anterior veíamos una de las posibles consecuencias de no actualizar el software que utilizamos.  Si bien es cierto que actualizar indiscriminadamente puede ser un tanto riesgoso en entornos de producción, (podríamos romper alguna dependencia y dejar inútil una aplicación) actualizar ES NECESARIO!!!

Actualizar puede ser una tarea simple y hasta divertida cuando se tratan de unos pocos servidores, pero ya no tanto cuando el número comienza a ascender! Por lo tanto la idea de esta entrada es definir un mecanismo que nos permita:

-Identificar entre actualizaciones de seguridad y el resto.
-Automatizar los avisos de actualizaciones.

El modelo es el siguiente:

-Utilizamos nagios para recolectar y presentar la información.
-No buscaremos actualizaciones cada 5 minutos, las actualizaciones no salen con tanta frecuencia por lo tanto estaríamos consumiendo ancho de banda de manera innecesaria. En el esquema planteado los servidores buscaran sus actualizaciones una vez en el día, y el resto del día mostraran los datos de esa recolección hasta que sean actualizados.
-Tendremos 2 scripts:
  1. updateNombreDelSO.pl que dependerá del sistema operativo y será el que se ejecute automáticamente una vez al día para recolectar las actualizaciones disponibles. Este script generará el segundo.
  2. check_actualizaciones.sh que será el script que ejecuté nrpe en los servidores, bajo la demanda del servidor corriendo nagios. Este script es la salida del primero, solo imprime la cantidad de actualizaciones pendientes de seguridad y extra, y un código de salida acorde a la situación.
-Definimos como CRITICAL la situación en la que existen actualizaciones de seguridad sin aplicar, y como WARNING cuando solamente existen actualizaciones extras (no de seguridad) por aplicar.

updateUbuntu.pl

#!/usr/bin/perl
$STATUS_OK=0;
$STATUS_WARNING=1;
$STATUS_CRITICAL=2;
$STATUS_UNKNOWN=3;
$PATCHS=`/usr/lib/update-notifier/apt-check 2>&1`;
@A=split(';',$PATCHS);
$SALIDA="#!/bin/bash\n";
$EXIT=$STATUS_UNKNOWN;
$FILE="/usr/lib/nagios/plugins/check_actualizaciones.sh";
if($A[0] eq "0" and $A[1] eq "0")
{
    open(F,">$FILE");
    print F "$SALIDA"."echo \"Existen ".$A[0]." actualizaciones.\"\n";
    print F "exit $STATUS_OK";
    close(F);
    chmod (0777,$FILE);
    exit;
}
if($A[0] > 0)
{
    $SALIDA = $SALIDA . "echo \"ERROR - Existen ".$A[0]." actualizaciones de seguridad y ".$A[1]." extras\"\n";
    $EXIT = $STATUS_CRITICAL;
}
else
{
    $SALIDA = $SALIDA . "echo \"WARNING - Existen ".$A[1]." actualizaciones extras.\"\n";
    $EXIT = $STATUS_WARNING;
}
open(F,">$FILE");
print F $SALIDA;
print F "exit $EXIT";
close(F);
chmod (0777,$FILE);

El script es bastante, sencillo,  "/usr/lib/update-notifier/apt-check" nos devuelve una linea de la forma act_seguridad;act_extras, que separamos con split y guardamos así el número de actualizaciones de seguridad en $A[0] y el número de actualizaciones extras en $A[1]. Luego de eso simplemente definimos la salida según las actualizaciones que haya para hacer. La salida de este script es un archivo bash con el formato:

#!/bin/bash
echo "ERROR -  Existen 3 actualizaciones de seguridad y 2 extras"
exit 2

 Este archivo es el que ejecutará nrpe cada vez que nagios consulte por el plugin, por lo tanto debemos agregarlo al archivo de configuracion de nrpe:

#echo "command[check_actualizaciones]=/usr/lib/nagios/plugins/check_actualizaciones.sh">> /etc/nagios/nrpe.cfg

Ahora nos encargamos de que ĺa recolección de actualizaciones se haga una vez al día:

#echo "@daily root /path/to/script/updateUbuntu.pl" >> /etc/crontab

No olvidarse de reiniciar nrpe una vez que se agregue la linea y se ejecute manuelmente por primera vez "/path/to/script/updateUbuntu.pl".

updateCentOS.pl

Para este script necesitamos instalar un agregado de yum, ejecutar como root:  "yum -y install yum-security" .

#!/usr/bin/perl

$STATUS_OK=0;
$STATUS_WARNING=1;
$STATUS_CRITICAL=2;
$STATUS_UNKNOWN=3;

$SALIDA="#!/bin/bash\n";
$EXIT=$STATUS_UNKNOWN;
$FILE="/usr/lib/nagios/plugins/check_actualizaciones.sh";
@PATCHS=`yum -q --security check-update 2>/dev/null`;

$A[0]=0;
$A[1]=0;
foreach $i (@PATCHS)
{
    $A[0]=$A[0]+1;  
}

@PATCHS=`yum -q check-update 2>/dev/null`;
foreach $i (@PATCHS)
{
    $A[1]=$A[1]+1;  
}
if($A[0] eq "0" and $A[1] eq "0")
{
        open(F,">$FILE");
        print F "$SALIDA"."echo \"Existen ".$A[0]." actualizaciones.\"\n";
        print F "exit $STATUS_OK";
        close(F);
        chmod (0777,$FILE);
        exit;
}
if($A[0] > 0)
{
       $SALIDA = $SALIDA . "echo \"ERROR - Existen ".$A[0]." actualizaciones se seguridad y ".$A[1]." extras\"\n";
       $EXIT = $STATUS_CRITICAL;
}
else
{
        $SALIDA = $SALIDA . "echo \"WARNING - Existen ".$A[1]." actualizaciones extras.\"\n";
        $EXIT = $STATUS_WARNING;
}
open(F,">$FILE");
print F $SALIDA;
print F "exit $EXIT";
close(F);
chmod (0777,$FILE);

Este script mantiene el mismo concepto, salvo que en lugar de ejecutar un solo programa ejecuta dos, el primero obtiene las actualizaciones de seguridad disponibles y el segundo las extras.

También debemos agregar la linea en el archivo de configuración de nrpe como se explicó antes, así como también la línea en crontab para la ejecución. Reiniciar nrpe y ejecutarlo manualmente la primera vez.

updateOpenSuSE.pl

#!/usr/bin/perl

$STATUS_OK=0;
$STATUS_WARNING=1;
$STATUS_CRITICAL=2;
$STATUS_UNKNOWN=3;

@PATCHS=`zypper -q lp 2>/dev/null | grep -i needed`;
$FLAG=0;
$c=0;

$SALIDA="#!/bin/bash\n";
$EXIT=$STATUS_UNKNOWN;
$FILE="/usr/lib/nagios/plugins/check_actualizaciones.sh";

foreach $i (@PATCHS)
{
    $c=$c+1;
    @aux=split('\|',$i);
    #solo uso los campos 1, 2 y 3
    if($i =~ /security/)
    {
        push(@SEGURIDAD,$aux[1]);
    }
    else
    {
        push(@OTROS,$aux[1]);
    }
    $FLAG=1;
}

if(@SEGURIDAD == 0 and @OTROS == 0)
{
        open(F,">$FILE");
        print F "$SALIDA"."echo \"Existen ".@SEGURIDAD." actualizaciones.\"\n";
        print F "exit $STATUS_OK";
        close(F);
        chmod (0777,$FILE);
        exit;
}

if(@SEGURIDAD > 0)
{
        $SALIDA = $SALIDA . "echo \"ERROR - Existen ".@SEGURIDAD." actualizaciones de seguridad y ".@OTROS." extras\"\n";
        $EXIT = $STATUS_CRITICAL;
}
else
{
        $SALIDA = $SALIDA . "echo \"WARNING - Existen ".@OTROS." actualizaciones extras.\"\n";
        $EXIT = $STATUS_WARNING;
}

open(F,">$FILE");
print F $SALIDA;
print F "exit $EXIT";
close(F);
chmod (0777,$FILE);


Nuevamente pero esta vez para openSuSE, también se deben aplicar los puntos de los otros dos casos.

No voy a incluir la configuración de nagios (por tiempo, ya me dio sueño :P, si alguien la precisa la tengo), pero básicamente consiste en definir un servicio y utilizar nrpe para ejecutar remotamente "check_actualizaciones.sh", de esta forma nagios nos estaría avisando cuántas actualizaciones pendientes tenemos y de qué tipo son.

Cabe aclarar que si actualizamos un servidor, las alertas no se irán hasta que no se vuelva a hacer la recolección, pero como esto pasa una vez al día estaríamos viendo alertas el resto del día. Por lo tanto recomiendo que una vez actualizado el servidor se ejecute manualmente el archivo de recolección para que las alarmas se vayan :D.

Concejos, dudas y demás serán bien recibidos!


jueves, 25 de agosto de 2011

Como quitar Windows 7, instalar CentOS 6 desde un pendrive y todo esto sin que se muera Ubuntu 9.10 oO


Windows en las particiones sda3 y sda4, CRAP!!! ¿sda2 es booteable? qué pasa si la borro?

root@moon:/home/juan# fdisk -lu /dev/sda

Disco /dev/sda: 250.1 GB, 250059350016 bytes
255 cabezas, 63 sectores/pista, 30401 cilindros, 488397168 sectores en total
Unidades = sectores de 1 * 512 = 512 bytes
Identificador de disco: 0xbab21f87

Dispositivo Inicio    Comienzo      Fin      Bloques  Id  Sistema
/dev/sda1              63   454623434   227311686   83  Linux
/dev/sda2   *   454625280   454830079      102400    7  HPFS/NTFS
/dev/sda3       454830080   476534783    10852352    7  HPFS/NTFS
/dev/sda4       476536095   488392064     5927985    5  Extendida
/dev/sda5       476536158   488392064     5927953+  82  Linux swap / Solaris


Para ver en qué consiste mi grub actualmente ejecuté

root@moon:/home/juan# grub-mkconfig -o grub.conf.bak
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.31-22-generic
Found initrd image: /boot/initrd.img-2.6.31-22-generic
Found linux image: /boot/vmlinuz-2.6.31-21-generic
Found initrd image: /boot/initrd.img-2.6.31-21-generic
Found linux image: /boot/vmlinuz-2.6.31-20-generic
Found initrd image: /boot/initrd.img-2.6.31-20-generic
Found linux image: /boot/vmlinuz-2.6.31-19-generic
Found initrd image: /boot/initrd.img-2.6.31-19-generic
Found linux image: /boot/vmlinuz-2.6.31-17-generic
Found initrd image: /boot/initrd.img-2.6.31-17-generic
Found linux image: /boot/vmlinuz-2.6.31-16-generic
Found initrd image: /boot/initrd.img-2.6.31-16-generic
Found linux image: /boot/vmlinuz-2.6.31-15-generic
Found initrd image: /boot/initrd.img-2.6.31-15-generic
Found linux image: /boot/vmlinuz-2.6.31-14-generic
Found initrd image: /boot/initrd.img-2.6.31-14-generic
Found memtest86+ image: /boot/memtest86+.bin
Found Windows 7 (loader) on /dev/sda2
done

De esta manera me hice con una copia de la configuración actual, por si las moscas!!!
mmmm... viendo un poco la configuración actual de grub encontré la parte donde se define el arranque del win7

menuentry "Windows 7 (loader) (on /dev/sda2)" {
        insmod ntfs
        set root=(hd0,2)
        search --no-floppy --fs-uuid --set 2860188560185c3e
        chainloader +1
}


Entonces decidí actuar! :P
Eliminé ambas particiones de windows!

root@moon:/etc/grub.d# fdisk -lu /dev/sda

Disco /dev/sda: 250.1 GB, 250059350016 bytes
255 cabezas, 63 sectores/pista, 30401 cilindros, 488397168 sectores en total
Unidades = sectores de 1 * 512 = 512 bytes
Identificador de disco: 0xbab21f87

Dispositivo Inicio    Comienzo      Fin      Bloques  Id  Sistema
/dev/sda1              63   454623434   227311686   83  Linux
/dev/sda4       476536095   488392064     5927985    5  Extendida
/dev/sda5       476536158   488392064     5927953+  82  Linux swap / Solaris
Por lo tanto me quedó un gran vacío xD, unos 11Gb perdidos entre sda1 y sda4, ese vacío debería ser llenado por CentOS :D.
Volví a lanzar grub-mkconfig -o grub.conf.bak.2, y esta vez ya no aparecieron las líneas del arranque de windows 7 :D.

root@moon:/etc/grub.d# grub-mkconfig -o grub.conf.bak.2
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.31-22-generic
Found initrd image: /boot/initrd.img-2.6.31-22-generic
Found linux image: /boot/vmlinuz-2.6.31-21-generic
Found initrd image: /boot/initrd.img-2.6.31-21-generic
Found linux image: /boot/vmlinuz-2.6.31-20-generic
Found initrd image: /boot/initrd.img-2.6.31-20-generic
Found linux image: /boot/vmlinuz-2.6.31-19-generic
Found initrd image: /boot/initrd.img-2.6.31-19-generic
Found linux image: /boot/vmlinuz-2.6.31-17-generic
Found initrd image: /boot/initrd.img-2.6.31-17-generic
Found linux image: /boot/vmlinuz-2.6.31-16-generic
Found initrd image: /boot/initrd.img-2.6.31-16-generic
Found linux image: /boot/vmlinuz-2.6.31-15-generic
Found initrd image: /boot/initrd.img-2.6.31-15-generic
Found linux image: /boot/vmlinuz-2.6.31-14-generic
Found initrd image: /boot/initrd.img-2.6.31-14-generic
Found memtest86+ image: /boot/memtest86+.bin
done


Por último una actualizada del grub para que los cambios se reflejen en el archivo de configuración real:

root@moon:/etc/grub.d# update-grub
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.31-22-generic
Found initrd image: /boot/initrd.img-2.6.31-22-generic
Found linux image: /boot/vmlinuz-2.6.31-21-generic
Found initrd image: /boot/initrd.img-2.6.31-21-generic
Found linux image: /boot/vmlinuz-2.6.31-20-generic
Found initrd image: /boot/initrd.img-2.6.31-20-generic
Found linux image: /boot/vmlinuz-2.6.31-19-generic
Found initrd image: /boot/initrd.img-2.6.31-19-generic
Found linux image: /boot/vmlinuz-2.6.31-17-generic
Found initrd image: /boot/initrd.img-2.6.31-17-generic
Found linux image: /boot/vmlinuz-2.6.31-16-generic
Found initrd image: /boot/initrd.img-2.6.31-16-generic
Found linux image: /boot/vmlinuz-2.6.31-15-generic
Found initrd image: /boot/initrd.img-2.6.31-15-generic
Found linux image: /boot/vmlinuz-2.6.31-14-generic
Found initrd image: /boot/initrd.img-2.6.31-14-generic
Found memtest86+ image: /boot/memtest86+.bin
done


Y ahora le pegamos un reboot a ver qué pasa!!! (no hacer bungee jumping con los cordones!!!, por las dudas tengan un live-cd de alguna distro para arreglar grub :P, yo tengo en un pendrive un ubuntu 9.04 por si las moscas).

NOTA: luego de reiniciar noté que ya no me aparecía la lista de entradas para elegir, sino que directamente me cargó la primer entrada sin decir nada. Me queda averiguar porque...

Bien ahora, a preparar el pendrive desde donde bootear para instalar CentOS, claramente no pienso quemar un CD para usarlo una sola vez ajajaja. Rata? no!, económico! Lo mas complicado de todo esto fue encontrar el pendrive xD, los hacen tan pequeñosss.

NOTA: Backup del pendrive!!!!

Para hacer booteable nuestro pendrive con una iso de CentOS , primero descargamos la iso (wget http://centos-mirror.hostdime.com.br/centos/6.0/isos/x86_64/CentOS-6.0-x86_64-minimal.iso) :P, y luego instalamos "UNetbootin" (a buscar los paquetes gente, aptitude search unetbootin y ver :P).
Elegimos la iso y la partición del pendrive que correspondan, aceptamos y esperamos un raaaato.



Ahora reiniciamos y vemos qué pasa!

Una vez booteado del pendrive elegimos la primer opción que dice con video básico y cargará el instalador de CentOS.

NOTA1: no encontraba el archivo /images/install.img, cuando aparece la opción para ingresar el path del archivo hay que poner ./images/install.img y elegir el dispositivo que corresponda al pendrive!

NOTA2: también es necesario copiar la iso del CD dentro del pendrive!

Durante la instalación en ningún momento hay que dejar que particione el disco ni que instale un bootloade!!!! sino no va a salir caro :P

Terminada la instalación, inicié ubuntu y corrí nuevamente un grub-mkconfig pero la configuración ni se enteró de la instalación de CentOS...

A meterle mano a Grub: la versión de grub que tengo ¿1.97? oO, se configura a partir de unas plantillas que se encuentran en el directorio /etc/grub.d y también el archivo /etc/default/grub.

Primero modificamos /etc/default/grub para que vuelva a aparecer el menu de sistemas operativos durante el booteo de grub, esto se logra comentando la línea que dice GRUB_HIDDEN_TIMEOUT=0.

Ahora agregamos las lineas necesarias para el arranque de CentOS en el archivo /etc/grub.d/40_custom (en este archivo deberiamos incluir todas las entradas que pongamos manualmente):

menuentry "CentOS 6" {
    set root=(hd0,2)
    linux /boot/vmlinuz-2.6.32-71.el6.x86_64 ro root=/dev/sda2 rhgb quiet
    initrd /boot/initramfs-2.6.32-71.el6.x86_64.img
}


NOTA: estas líneas deben ser agregadas al final del archivo y sin modificar el resto de las lineas anteriores.

Vemos que las lineas indican la partición de root, la imagen del kernel y el initrd.
Ahora debemos actualizar grub para que aplique estos cambios a la configuración actual (update-grub) y vemos que la entrada fue agregada correctamente al final del archivo de configuración:

root@moon:/home/juan# tail /boot/grub/grub.cfg
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
menuentry "CentOS 6" {
    set root=(hd0,2)
    linux /boot/vmlinuz-2.6.32-71.el6.x86_64 ro root=/dev/sda2 rhgb quiet
    initrd /boot/initramfs-2.6.32-71.el6.x86_64.img
}

### END /etc/grub.d/40_custom ###


Reiniciamos y.... :D un lujo, probado y re contra probado, levantó CentOS sin ningún inconveniente.