jeudi 12 juillet 2012

Debugging with strace

There are times when your software is failing but it doesn't output any error messages. Maybe you are getting a 500 error when you load a cgi script but the log is not telling you why. Strace can often help in situations like this.

Strace does not trace the internals of your application, it only knows about syscalls, or calls into the kernel. This can be enough in many cases to show you when your application might be trying to look for a file that is not there, or when it cannot allocate memory or some other error.

Generally I call strace with option -f so it will follow child processes, and then the name of the command to trace. To save the output to a file, use -o.

Code:
[josephw@myhost ~]$ strace -f -o strace.out ls /
bin   dev  home    lib         media  mnt  proc  sbin     space  sys  usr
boot  etc  initrd  lost+found  misc   opt  root  selinux  srv    tmp  var


[josephw@myhost ~]$ less strace.out
6368  execve("/bin/ls", ["ls", "/"], [/* 17 vars */]) = 0
6368  uname({sys="Linux", node="dss03.spry.com", ...}) = 0
6368  brk(0)                            = 0x8cc5000
6368  access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
6368  open("/etc/ld.so.cache", O_RDONLY) = 3
6368  fstat64(3, {st_mode=S_IFREG|0644, st_size=36486, ...}) = 0
6368  old_mmap(NULL, 36486, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f8f000
6368  close(3)                          = 0
6368  open("/lib/tls/librt.so.1", O_RDONLY) = 3

....output trimmed....
The first column shows the process id, and then you see the syscall and its result. Here it opens /etc/ld.so.cache in readonly mode, and the result is file descriptor 3. After this it calls fstat64 and old_mmap on file descriptor 3. Then it closes file descriptor 3. Once it has been closed, it can be reused as you see in the next call where it opens /lib/tls/librt.so.1.

In cases where you want to trace a process that is already running, pass the -p option to strace. I will run strace on Apache.
Code:
[josephw@myhost ~]$ ps aux |grep http
apache    6457  0.0  0.5 18764 5456 ?        S    Oct31   0:00 /usr/sbin/httpd
apache    6498  0.0  0.5 18764 5464 ?        S    Oct31   0:00 /usr/sbin/httpd
apache    6500  0.0  0.5 18768 6000 ?        S    Oct31   0:00 /usr/sbin/httpd
apache    6505  0.0  0.5 18768 6080 ?        S    Oct31   0:00 /usr/sbin/httpd
apache    6507  0.0  0.5 18764 5464 ?        S    Oct31   0:00 /usr/sbin/httpd
Here I have the process IDs for apache running on my box. Which one to trace? Pick one, then keep reloading your web site until the process you are tracing is the one that serves up your page. If the process does not belong to you, you will probably need to be root in order to trace it.
Code:
[root@myhost ~]# strace -f -o strace.out -p 6457 &
[root@myhost ~]# tail -f strace.out

....output trimmed....

21361 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
21361 open("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/tls/i686/sse2/libperl.so", O_RDONLY) = -1 ENOENT
 (No such file or directory)
21361 stat64("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/tls/i686/sse2", 0xbfeca780) = -1 ENOENT (No su
ch file or directory)
21361 open("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/tls/i686/libperl.so", O_RDONLY) = -1 ENOENT (No
such file or directory)
21361 stat64("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/tls/i686", 0xbfeca780) = -1 ENOENT (No such fi
le or directory)
21361 open("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/tls/sse2/libperl.so", O_RDONLY) = -1 ENOENT (No
such file or directory)
21361 stat64("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/tls/sse2", 0xbfeca780) = -1 ENOENT (No such fi
le or directory)
21361 open("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/tls/libperl.so", O_RDONLY) = -1 ENOENT (No such
file or directory)
21361 stat64("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/tls", 0xbfeca780) = -1 ENOENT (No such file or
 directory)
21361 open("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/i686/sse2/libperl.so", O_RDONLY) = -1 ENOENT (No
 such file or directory)
21361 stat64("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/i686/sse2", 0xbfeca780) = -1 ENOENT (No such f
ile or directory)
21361 open("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/i686/libperl.so", O_RDONLY) = -1 ENOENT (No such
 file or directory)
21361 stat64("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/i686", 0xbfeca780) = -1 ENOENT (No such file o
r directory)
21361 open("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/sse2/libperl.so", O_RDONLY) = -1 ENOENT (No such
 file or directory)
21361 stat64("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/sse2", 0xbfeca780) = -1 ENOENT (No such file o
r directory)
21361 open("/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so", O_RDONLY) = 3

....output trimmed....
I ran strace in the background by using & so I could view the output of strace.out with tail -f. This way I could know when my process was the one that was doing the work. Then I reloaded my site about 5 times until it cycled around to process 6457.

In the beginning of the output shown, it may look like there is an error, because it keeps showing "No such file or directory" while trying to load libperl.so. But you can see that it is looking for the library in multiple paths and eventually it does find it in the directory /usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/. In this case, the page was loaded without any errors. I will do it again, but this time I will try to load a page that does not exist, in this case http://10.2.4.15/test.
Code:
....output trimmed....

6457  lstat64("/usr/local/www/test", 0xbffb3104) = -1 ENOENT (No such file or directory)

....output trimmed....

6457  read(18, 0xbffb3270, 512)         = -1 ECONNRESET (Connection reset by peer)
6457  close(18)                         = 0
6457  read(5, 0xbffb347f, 1)            = -1 EAGAIN (Resource temporarily unavailable)
6457  semop(7307267, 0x1c9740, 1 <unfinished ...>
It tried to find /usr/local/www/test but could not so it failed in the end.
 
Source : http://forums.spry.com/howtos/1502-debugging-strace.html

vendredi 29 juin 2012

How to install nTOP 4 on CentOS 6 by RPM

nTOP is a Network Tap program. It displays a summary of network usage by machines on your network in a format reminiscent of the unix top utility. It can also be run in web mode, which allows the display to be browsed with a web browser.
1. Install RPMforge repo :
# rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
Note : you can find the lastest of rpmforge repo at http://pkgs.repoforge.org/rpmforge-release
2. Install necessary packages for nTOP :
# yum install graphviz rrdtool rrdtool-devel geoip geoip-devel net-snmp-libs gdbm gdbm-devel
3. Install nTOP :
# cd /usr/src
# wget http://dl.marmotte.net/rpms/redhat/el6/x86_64/ntop-4.0.3-1.el6/ntop-4.0.3-1.el6.x86_64.rpm
# rpm -ivh ntop-4.0.3-1.el6.x86_64.rpm
# chkconfig ntop on
4. Set Admin password :
# ntop -A
5. Start nTOP service :
# service ntop start
6. Confirm nTOP is working :
# lsof -i :3000
# lsof -i :3001
Note : TCP 3000 for HTTP and TCP 3001 for HTTPs
7. Secure ntop access from outside :
a. Browse to https://ip-address.your.nTOP.server:3001
b. Click “Admin” > “Configure” > “Protect URLs”
c. Enter username “admin” and your password (set in step 4 above) when prompted.
d. Click “Add URL” then “Add URL” again.
NOTE: This will require a username and password to access https://ip-address.your.nTOP.server:3001

source  :  http://saroot.org/blog/how-to-install-ntop-4-on-centos-6-by-rmp-package/

jeudi 1 mars 2012

Cacti on Centos 5.7 en 10 étapes

1) Installer CentOS
2) Ajouter le dépot rpmforge :

wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm

rpm -Uvh rpmforge-release-0.5.2-2.el5.rf.i386.rpm

3) Installer cacti et mysql-server :

yum install cacti mysql-server

4) Démarrer mysql-server :

/etc/init.d/mysql-server start

5) Config de sql-server :

/usr/bin/mysqladmin -u root password 'yourpassword'
/usr/bin/mysqladmin -u root -p create cacti

puis,

[root@cacti-appliance ~]# /usr/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.0.95 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> GRANT ALL ON cacti.* TO 'cactiuser'@'localhost' IDENTIFIED BY 'CACTIUSER-PASSWORD-GOES-HERE';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

5) Création de la base sql : 

/usr/bin/mysql cacti -u cactiuser -p < /var/www/cacti/cacti.sql

6) Modifier /var/www/cacti/include/config.php en indiquant le bon utilisateur (cactiuser) et le mot de passe


7) Modifier le fichier /etc/httpd/conf.d/cacti.conf pour permettre l'accès au serveur depuis d'autres machines/réseau

8) Démarrer le serveur web :  /etc/init.d/httpd start

9) Naviguer à l'adresse http://@IPduserveur/cacti/

10) Se laisser guider...

mercredi 30 novembre 2011

La recherche d'images inversées

Afin de savoir où l'on peut retrouver une image donnée sur sur le web, il existe des moteurs de recherche inversé.
Deux manières de faire :
- indiquer l'url de l'image
- uploader l'image recherchée dans le moteur

Plusieurs sites proposent ce système : Tineye et bien sur Google Image (en cliquant sur l'appareil photo afin de charger une image ou indiquer une url)





lundi 24 octobre 2011

Masquer son numéro de téléphone sans modifier la configuration de son téléphone

Il est facile de masquer son numéro sans pour autant modifier les paramètres de son téléphone.


Pour ce faire, il faut juste ajouter le préfixe #31# avant le numéro à composer.

vendredi 21 octobre 2011

Comment tomber directement sur le répondeur de son interlocuteur

Il est possible de tomber directement sur le répondeur d'un abonné SFR/Orange/Bouygues.








1) Identifier l'opérateur associé au numéro de téléphone. Il y a encore quelques années il était possible de déduire l'opérateur en se basant sur le début du numéro de téléphone. Avec la portabilité du numéro, cette méthode est de plus en plus aléatoire.

Quelques exemples :

SFR :
0603
0605
0606
0609 à 0629
0634 à 0636
0655

ORANGE :
0607 à 0608
0630 à 0633
0637
0642
0643
0645
0654
0670 à 0689

Bouygues Telecom :
0650
0653
0659
0660 à 0669
0698 à 0699

  • Est ce un abonné SFR ? https://monprofil.sfr.fr/monprofilWEB/publique/PNMAccueil
  • Est ce un abonné Orange ?
  • Est ce un abonné Bouygues ?
2) Ensuite, en fonction de l'opérateur la marche à suivre est différente :
  • Orange, composer le 06 80 80 80 80 
  • SFR, composer le  06 1000 1000
  • Bouygues, composer le 06 60 66 0001 
Dans les 3 cas, après avoir composer le numéro: il suffira ensuite de se laisser guider par le système qui demandera de composer le numéro de téléphone de la personne à qui l’ on veut laisser un message vocal sur le répondeur… et hop, on accède directement à la messagerie vocale sans que le téléphone de la personne ait sonné. 

Coté coût, c'est le même tarif que de téléphoner sur un portable.

 A noter qu'il existe des méthodes vraiment payante facilement trouvable sur le net.

mercredi 19 octobre 2011

Change timezone on CentOS - RedHat - Fedora

For exemple, for Paris :

cp /usr/share/zoneinfo/Europe/Paris /etc/localtime

Enjoy :)