Tag Archives: GNU

Controlling CPU frequency

We can control the CPU frequency with the cpufrequtils package. Installation is quite easy.

$ sudo aptitude install  cpufrequtils

To check to see if it’s installed.

[06:41:32] xavi@debian: ~ $ lsmod | grep -i cpu
acpi_cpufreq            6796  1 
cpufreq_powersave       1856  0 
cpufreq_stats           3776  0 
cpufreq_ondemand        6476  1 
freq_table              4224  3 acpi_cpufreq,cpufreq_stats,cpufreq_ondemand
cpufreq_userspace       3172  0 
cpufreq_conservative     5960  0 
processor              32576  4 acpi_cpufreq,thermal
[06:45:26] xavi@debian: ~ $ 

Continue reading

Apache visits with Perl

Here is another Perl script. This one given an Apache log file will tell you the number of hits per IP. Here is the code.

#!/usr/bin/perl -w

use strict;
use diagnostics;

if( $#ARGV != 0 ){
        print "Usage: ./parser.pl <logfile>\n"; 
        exit 1; }

# Opening logfile
open FILE, "$ARGV[0]" or die $!;

# Defining variables
my @lines = <FILE>;
my @ip;
my $items = scalar(@lines);
my ($i, $value); 
my %hits;

# Removing IP addresses from logfile and counting the hits per IP.
for ($i = 0; $i < $items; $i++)
        { @ip = split (/ /, $lines[$i]);
        if (exists $hits{$ip[0]}) {
                $hits{$ip[0]} = $hits{$ip[0]}+1; 
                chomp($hits{$ip[0]}); } 
        else {  $hits{$ip[0]} = 1; } 
        }

# Ordering and printing the hits per IP.
foreach $value (sort {"$hits{$b}" <=> "$hits{$a}" } keys %hits)
        { print "$value made $hits{$value} hits.\n"; }

Here is an usage example.

xavi@debianserver:~/Perl$ ./parser.pl /tmp/apachelog 
12.130.86.9 made 106 hits.
208.111.39.192 made 89 hits.
67.195.114.231 made 42 hits.
98.14.22.97 made 27 hits.
208.54.45.78 made 17 hits.
213.5.71.12 made 16 hits.
199.106.237.37 made 15 hits.
66.249.67.212 made 15 hits.
208.54.45.62 made 12 hits.
87.250.252.242 made 9 hits.
174.121.74.234 made 6 hits.
188.187.102.74 made 6 hits.
208.54.45.72 made 5 hits.
64.40.121.184 made 4 hits.
206.196.125.114 made 3 hits.
207.46.13.94 made 2 hits.
220.181.146.169 made 2 hits.
142.166.170.101 made 2 hits.
220.181.94.225 made 2 hits.
142.166.170.100 made 2 hits.
65.52.108.60 made 2 hits.
207.46.199.182 made 2 hits.
207.46.13.101 made 2 hits.
208.54.45.56 made 2 hits.
209.2.233.223 made 1 hits.
67.210.218.102 made 1 hits.
66.249.67.66 made 1 hits.
220.181.7.54 made 1 hits.
207.46.199.199 made 1 hits.

As usual, suggestions are always welcome.

Some basic MySQL

I don’t feel like doing a long post today so I’ll do some basic MySQL. First lets change the MySQL prompt. Here is a default MySQL prompt.

mysql> use test;
Database changed
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2010-08-17 06:41:00 | 
+---------------------+
1 row in set (0.00 sec)

mysql> 

Pretty boring, right? Lets spice this a little bit. Type prompt mysql \u@\d> from the MySQL prompt and lets see what happens.

mysql> prompt mysql \u@\d>
PROMPT set to 'mysql \u@\d>'
mysql root@test> select now() \G
*************************** 1. row ***************************
now(): 2010-08-17 06:44:56
1 row in set (0.00 sec)

mysql root@test>

What we just did is modify the MySQL prompt to show us the user and the database being used. This is much more informative than the previous MySQL prompt.

Now what if we wanted to know the uptime of the database. Pretty simple type status; and that will give us the time the database has been up.

 mysql root@test>status;
--------------
mysql  Ver 14.12 Distrib 5.0.75, for debian-linux-gnu (i486) using readline 5.2

Connection id:          39
Current database:       test
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.0.75-0ubuntu10.5-log (Ubuntu)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
UNIX socket:            /var/run/mysqld/mysqld.sock
Uptime:                 10 min 54 sec

Threads: 1  Questions: 125  Slow queries: 0  Opens: 34  Flush tables: 1  Open tables: 28  Queries per second avg: 0.191
--------------

mysql root@test>

If we wanted to know the running processes just type: show full processlist \G.

mysql root@test>show full processlist \G
*************************** 1. row ***************************
     Id: 39
   User: root
   Host: localhost
     db: test
Command: Query
   Time: 0
  State: NULL
   Info: show full processlist
1 row in set (0.00 sec)

mysql root@test>

This is all. I know this is very simple stuff, but didn’t felt like doing a long and complex post. As usual suggestions are always welcome.
References:

  1. https://www.ultraquantix.com/blog/2008/12/making-the-mysql-prompt-more-useful/”
  2. http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html

Tomcat MySQL realm

Let’s get started with another Tomcat chapter in this blog. Tomcat comes by default with the UserDatabaseRealm which is really crappy because it reads user info from tomcat-users.xml file and a Tomcat restart is needed for the changes to take effect. This is not appropriate for a production environment so we are going to authenticate against a MySQL database instead of the tomcat-users.xml file.
Continue reading

Having two Tomcat JVMs

Let’s say you want to have two Tomcat JVMs running. How do we do it? It’s quite easy. We only need to copy some directories and modify the ports were we want Tomcat to run. We previously showed how to install Tomcat. Lets create the directory where we will make the new JVM run.

tomcat@debian:~$ mkdir /opt/tomcat/srva
tomcat@debian:~$

Now we copy the webapps, conf and bin directories to /opt/tomcat/srva and create some directories in /opt/tomcat/srva.

tomcat@debian:/opt/tomcat$ cp -R webapps/ /opt/tomcat/srva/
tomcat@debian:/opt/tomcat$ cp -R conf/ /opt/tomcat/srva/
tomcat@debian:/opt/tomcat$ cp -R bin/ /opt/tomcat/srva/
tomcat@debian:/opt/tomcat$ mkdir srva/logs srva/temp srva/shared srva/common srva/work

We are now going to change startup ports to 8081 instead of default 8080 in /opt/tomcat/conf/server.xml. Don’t forget the shutdown port too.

tomcat@debian:/opt/tomcat$ grep -i port srva/conf/server.xml 
<Server port="8006" shutdown="SHUTDOWN">
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
         Define a non-SSL HTTP/1.1 Connector on port 8080
    <Connector port="8081" protocol="HTTP/1.1" 
               redirectPort="8444" />
               port="8080" protocol="HTTP/1.1" 
               redirectPort="8443" />
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <!-- You should set jvmRoute to support load-balancing via AJP ie :
tomcat@debian:/opt/tomcat$

Now we need to define $CATALINA_BASE and $CATALINA_HOME. The way I did is by adding them to startup.sh in the srva/bin directory. Add the following to startup.sh.

CATALINA_HOME="/opt/tomcat/"
export CATALINA_HOME
CATALINA_BASE="/opt/tomcat/srva"
export CATALINA_BASE
JAVA_HOME="/opt/java"
export JAVA_HOME

We should now be ready to go. So lets get the party started.

tomcat@debian:/opt/tomcat$ echo "starting orig instance"; bin/startup.sh; echo "starting srva new instance"; srva/bin/startup.sh
starting orig instance
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /opt/java
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar
starting srva new instance
Using CATALINA_BASE:   /opt/tomcat/srva
Using CATALINA_HOME:   /opt/tomcat/
Using CATALINA_TMPDIR: /opt/tomcat/srva/temp
Using JRE_HOME:        /opt/java
Using CLASSPATH:       /opt/tomcat/srva/bin/tomcat-juli.jar:/opt/tomcat//bin/bootstrap.jar
tomcat@debian:/opt/tomcat$ ps auxwww | grep -i java | grep -v grep
tomcat    4759 10.4  1.3 1065228 50128 pts/0   Sl   12:37   0:03 /opt/java/bin/java -Djava.util.logging.config.file=/opt/tomcat
/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/tomcat/endorsed 
-classpath /opt/tomcat/bin/bootstrap.jar -Dcatalina.base=/opt/tomcat -Dcatalina.home=/opt/tomcat -Djava.io.tmpdir=/opt/tomcat/temp 
org.apache.catalina.startup.Bootstrap start
tomcat    4769 10.4  1.3 1064652 47872 pts/0   Sl   12:37   0:03 /opt/java/bin/java -Djava.util.logging.config.file=/opt/tomcat/srva/conf
/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/tomcat//endorsed 
-classpath /opt/tomcat/srva/bin/tomcat-juli.jar:/opt/tomcat//bin/bootstrap.jar -Dcatalina.base=/opt/tomcat/srva -Dcatalina.home=/opt
/tomcat/ -Djava.io.tmpdir=/opt/tomcat/srva/temp org.apache.catalina.startup.Bootstrap start
tomcat@debian:/opt/tomcat$

We see from the ps output that there are two java processes running. So lets check on what ports they are running. They should be on 8080 and 8081.

tomcat@debian:/opt/tomcat$ lsof -i :8080
COMMAND  PID   USER   FD   TYPE DEVICE SIZE NODE NAME
java    4759 tomcat   28u  IPv6  20974       TCP *:http-alt (LISTEN)
tomcat@debian:/opt/tomcat$ lsof -i :8081
COMMAND  PID   USER   FD   TYPE DEVICE SIZE NODE NAME
java    4769 tomcat   29u  IPv6  20980       TCP *:tproxy (LISTEN)
tomcat@debian:/opt/tomcat$

And this is the end of this how-to. As usual suggestions are always welcome.