Saturday, February 25, 2012

[UPDATE] Sonar with PostgreSQL

This documentation is based on:
http://docs.codehaus.org/display/SONAR/Install+Sonar

It was tested with Sonar 2.13.1

A. Getting Sonar and testing it

1. Get Sonar from:
http://www.sonarsource.org/downloads/

2. Unzip it to the
#---
unzip -x <sonar zip file> -d /opt/
#---


2.1. Example:
#---
unzip -x sonar-2.13.1.zip -d /opt/
#---


3. Start Sonar as standalone
#---
/opt/sonar-<version>/bin/linux-<arch>/sonar.sh start
#---


3.1. Example:
#---
/opt/sonar-2.13.1/bin/linux-x86-64/sonar.sh start
#---


4. Follow the logs to check that everything is ok:
#---
tail -f /opt/sonar-<version>/logs/sonar.log
#---


4.1. Example:
#---
tail -f /opt/sonar-2.13.1/logs/sonar.log
#---


NOTE: You are waiting for a line similar to:
INFO   | jvm 1    | 2012/02/25 17:16:31 | 2012-02-25 17:16:31.818:INFO::Started SelectChannelConnector@0.0.0.0:9000

5. Check that Sonar is actually accepting connections:
#---
netstat -nl --inet --inet6 | grep 9000
#---


NOTE: It should return something like:
tcp        0      0 :::9000                     :::*                        LISTEN

5.1. Open your browser to double check:
http://localhost:9000/

Login credentials:
username: admin
password: admin


B. Configuring Sonar to work with PostgreSQL

1. Check if PostgreSQL is running:
#---
systemctl status postgresql.service
#---


1.1. If not, just enable it and start it:
#---
systemctl enable postgresql.service
systemctl start postgresql.service
#---


1.2. Check if it has listeners running:
#---
netstat -nl --inet --inet6 | grep 5432
#---


NOTE: You are expecting something like this:
tcp        0      0 127.0.0.1:5432              0.0.0.0:*                   LISTEN      
tcp        0      0 ::1:5432                    :::*                        LISTEN   

[UPDATED]
2. Setup PostgreSQL
#---
createuser -U postgres -W -D -R -S sonar
psql -U postgres -W -c "ALTER USER sonar WITH PASSWORD 'sonar';"
createdb -U postgres -W -E UTF8 -O sonar sonar
psql -U postgres -W -c "GRANT ALL ON DATABASE sonar TO sonar;"
#---

[UPDATED]

2.1. Check connection:
#---
psql -U sonar -d sonar -W -c "SELECT 1;"
#---


3. Configure Sonar:

3.1. Edit the file /opt/sonar-<version>/conf/sonar.properties

After editing it must looks like the following (after issuing the below command):
#---
cat conf/sonar.properties | grep -v "^#" | grep -v "^[[:space:]]*$"
#---


sonar.jdbc.username:                       sonar
sonar.jdbc.password:                       sonar # if you gave another password this property must reflect it
sonar.jdbc.url:                            jdbc:postgresql://localhost/sonar
sonar.jdbc.driverClassName:                org.postgresql.Driver
sonar.jdbc.validationQuery:                select 1
sonar.jdbc.schema:                         public
sonar.jdbc.maxActive:                      20
sonar.jdbc.maxIdle:                        5
sonar.jdbc.minIdle:                        2
sonar.jdbc.maxWait:                        5000
sonar.jdbc.minEvictableIdleTimeMillis:     600000
sonar.jdbc.timeBetweenEvictionRunsMillis:  30000
sonar.notifications.delay=60

4. Check if Sonar is ok by restarting it:
#---
/opt/sonar-<version>/bin/linux-<arch>/sonar.sh restart
#---


4.1. Example:
#---
/opt/sonar-2.13.1/bin/linux-x86-64/sonar.sh restart
#---


4.2. Keep track of the log (see step 4 of section A)

5. Check if Sonar correctly accessed the database in PostgreSQL:
#---
psql -U sonar -d sonar -W -c "\\dt"
#---


NOTE: It currently has 47 tables and it yields it on the above command with the following line:
(47 rows)

C. Test your Sonar with any project you want (below what you need for Maven projects).

1. With Maven projects you will need the following pom.xml section:
<project>
 <!-- your pom initial setup -->
 <properties>
  <!-- your other properties -->
  <sonar.jdbc.url>jdbc:postgresql://localhost/sonar</sonar.jdbc.url>
  <sonar.jdbc.username>sonar</sonar.jdbc.username>
  <sonar.jdbc.password>sonar</sonar.jdbc.password>
  <sonar.jdbc.driver>org.postgresql.Driver</sonar.jdbc.driver>
 </properties>
 <!-- your pom remaining setup -->
</project>

2. Execute the Sonar target on your project:
#---
cd <maven project>
mvn clean sonar:sonar
#---

4 comments:

fati said...

bonjour, svp je suis bolquée dans cette étape depuis 2 jours quand je fait sa :
/opt/sonar-2.13.1/bin/linux-x86-64/sonar.sh start
ça marche sa me donne :
Starting sonar...
Started sonar.
aprés mon probléme est ici j'ai l'impression que sonar il demarre et arrete en meme temps quand je fait sa :
tail -f /opt/sonar-2.13.1/logs/sonar.log
m'affiche :
INFO | jvm 1 | 2012/03/20 17:25:40 | 2012-03-20 17:25:40.414:INFO::Shutdown hook executing
INFO | jvm 1 | 2012/03/20 17:25:40 | 2012-03-20 17:25:40.414:INFO::Graceful shutdown SelectChannelConnector@0.0.0.0:9000
INFO | jvm 1 | 2012/03/20 17:25:40 | 2012-03-20 17:25:40.415:INFO::Graceful shutdown org.mortbay.jetty.webapp.WebAppContext@b9ba20{/,file:/opt/sonar-2.13.1/war/sonar-server}
2012.03.20 17:25:41 INFO org.sonar.INFO Stop sonar...
INFO | jvm 1 | 2012/03/20 17:25:41 | 2012-03-20 17:25:41.416:INFO::Stopped SelectChannelConnector@0.0.0.0:9000
2012.03.20 17:25:46 INFO org.sonar.INFO Notification service stopped
2012.03.20 17:25:46 INFO org.sonar.INFO Embedded database stopped
2012.03.20 17:25:46 INFO org.sonar.INFO Stop sonar done: 5116 ms
INFO | jvm 1 | 2012/03/20 17:25:46 | 2012-03-20 17:25:46.640:INFO::Shutdown hook complete
STATUS | wrapper | 2012/03/20 17:25:47 | <-- Wrapper Stopped
voilà merci pour votre réponse !

Gustavo said...

Sorry, but I didn't get your point. I do not speak French. Can you translate it to English?

Thanks

fati said...

hello, please I'm bocked in this stage for 2 days when I made ​​her:
/opt/sonar-2.13.1/bin/linux-x86-64/sonar.sh start
it works it gives me:
Starting sonar...
Started sonar.
here is my problem after I get the impression that it starts and stops sonar at the same time when I made ​​her:
tail -f /opt/sonar-2.13.1/logs/sonar.log
displays:
INFO | jvm 1 | 2012/03/20 17:25:40 | 2012-03-20 17:25:40.414:INFO::Shutdown hook executing
INFO | jvm 1 | 2012/03/20 17:25:40 | 2012-03-20 17:25:40.414:INFO::Graceful shutdown SelectChannelConnector@0.0.0.0:9000
INFO | jvm 1 | 2012/03/20 17:25:40 | 2012-03-20 17:25:40.415:INFO::Graceful shutdown org.mortbay.jetty.webapp.WebAppContext@b9ba20{/,file:/opt/sonar-2.13.1/war/sonar-server}
2012.03.20 17:25:41 INFO org.sonar.INFO Stop sonar...
INFO | jvm 1 | 2012/03/20 17:25:41 | 2012-03-20 17:25:41.416:INFO::Stopped SelectChannelConnector@0.0.0.0:9000
2012.03.20 17:25:46 INFO org.sonar.INFO Notification service stopped
2012.03.20 17:25:46 INFO org.sonar.INFO Embedded database stopped
2012.03.20 17:25:46 INFO org.sonar.INFO Stop sonar done: 5116 ms
INFO | jvm 1 | 2012/03/20 17:25:46 | 2012-03-20 17:25:46.640:INFO::Shutdown hook complete
STATUS | wrapper | 2012/03/20 17:25:47 | <-- Wrapper Stopped
Thanks for your answer

Gustavo said...

Hi Fati,

I will need more lines than that and more information. The best way is for you send me your email on a following post. I get in touch and you can send me the whole log.

Cheers,

Gustavo