Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Thursday, November 23, 2017

How to find wireless network frequency

There are situations where we need to find out our wireless network frequency.
Eg:- before purchasing network devices we need to verify whether we are in a 2.4 or 5 GHz network

Usage of iwlist command on a linux box,
iwlist <WIRELESS INTERFACE> scanning | grep -C3 <WIRELES_NETWORK_NAME>


Eg:
 iwlist wlp4s0 scanning | grep -C3 VM6292804
                    Frequency:2.412 GHz (Channel 1)
                    Quality=47/70  Signal level=-63 dBm 
                    Encryption key:on
                    ESSID:"VM6292804"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s

Sunday, July 23, 2017

Running different MySQL Servers inside a single host

There are many circumstances where running different MySQL server versions required. Eg:- testing

Spinning MySQL docker instance is the easiest way I have found among multiple methods.
sudo docker run --name mysql56 -p 127.0.0.1:3316:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6

This command will download and spin a MySQL 5.6 container. At the same time it will map the default 3306 port in to the 3316 of the host machine.

Thursday, June 22, 2017

Download a part of YouTube video as mp3

I will be using ffmpeg and youtube-dl installed linux distro (Ubuntu 16.04 LTS)
ffmpeg -ss 00:00:00.0000 -i $(youtube-dl -f 22 --get-url <YOUTUBE_LINK>) -t 00:00:00.0000 -acodec libmp3lame -qscale:a 0 sample.mp3
Fill <YOUTUBE_LINK> with the link to YouTube video.

Find further explanation of above command here,
  • -ss : start of the stream 00:00:00.0000 stands for hour:minute:second.millisecond
  • -i   : provide the input, here we will be using an output from youtube-dl.
  • -t   : length of the stream

Further information about bitrate selection (-qscale:a 0) can be found here.
  • libmp3lame is the audio encoding used for this purpose.
  • sample.mp3 is the output file of above command.

Wednesday, November 26, 2014

Path to linux screen

Screen makes dev/devops life easy in many ways,

1. If you want to keep a shell active, even with network/ssh interrupts.
2. Resume previous shell from another location
3. Keep a long running process

i). Start named screen, I will explain why we need a name in later step.

screen -R <name> 
Eg : - screen -R udara

If you don't have screen installed on your system run,

sudo apt-get install screen

ii). De-attach from the current screen

ctrl+a+d , this will return you back to the default shell( where you start screen)

This is the interesting and powerful feature of shell, where you can De-attach or Re-attach to the same screen.

iii). Re-attach to the same screen

screen -r will re-attach you to the previous screen.

But let's assume we have multiple De-attached screens, screen -r will list down all existing screens in that case.

Eg :-

udara@udara-home:~$ screen -r
There are several suitable screens on:
    5192.udara    (2014-11-25 10:41:15)    (Detached)
    5135.JMeter    (2014-11-25 10:40:59)    (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.


This is where screen name comes handy :) You can easily recognize which screen you need to Re-attach. So use a meaningful name when starting your screen.

iv). Re-attach to a screen, when there are multiple De-attached screens

screen -r <screen-name>
Eg :-  screen -r 5192.udara

v). List existing screens 
Yes, If you have multiple screens screen -r will list down all existing screens. But there is a specific list command to do this.

screen -ls
screen -list


udara@udara-home:~$ screen -ls
There are screens on:
    5410.pts-7.udara-home    (2014-11-25 10:51:20)    (Detached)
    5192.udara    (2014-11-25 10:41:15)    (Detached)
    5135.JMeter    (2014-11-25 10:40:59)    (Detached)
3 Sockets in /var/run/screen/S-udara.



These are some basic commands which we can use to make our life easy, refer screen man page for more commands/shortcuts.

Wednesday, November 13, 2013

Benchmark your HTTP server

Problem : Send 10,000 requests to a simple node.js server with variable number of concurrent requests.

1st test  - send 100 concurrent requests
2nd test -  send 500 concurrent requests

Following is my simple node.js server,
var http = require('http');

http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Hello Blogger\n');
        }).listen(8000, '127.0.0.1');

console.log('Server running at http://127.0.0.1:8000/');
To overcome my initial problem I found this simple http benchmarking tool apache-bench aka ab. This comes packed with apache installation or you can simply install by giving sudo apt-get install apache2-utils on debian based distro.

Save above node server in ~/node_test/server/ directory as server.js.
cd to the server directory using terminal.
Before running the next step make sure you have node.js installed in your environment. If not use this git repo and the README to install node.js.

We bind our node based HTTP server to port 8000 because binding to ports lower than 1000, we need root access.

If you don't want to use node.js, you can use any HTTP server to try out apache-bench. I will move with my node server,

start node server using following command,
node server.js
You will see something similar,
udara@thinkPad:~/node_test/server$ node server.js
Server running at http://127.0.0.1:8000/
Lets run a simple test on our node server. Open another terminal and run,
ab -n 100 -c 50 http://127.0.0.1:8000/
-n Denotes number of requests to perform.
-c Denotes number of multiple requests to make(concurrency).
http://127.0.0.1:8000/ is my node server running in the same machine, on port 8000.

*Make sure you have the trailing / after port number.

This will run ab for 100 requests with 50 concurrent requests. If you have done everything right you will get something similar informative output,

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient).....done


Server Software:       
Server Hostname:        127.0.0.1
Server Port:            8000

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      50
Time taken for tests:   0.022 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      11300 bytes
HTML transferred:       1200 bytes
Requests per second:    4624.06 [#/sec] (mean)
Time per request:       10.813 [ms] (mean)
Time per request:       0.216 [ms] (mean, across all concurrent requests)
Transfer rate:          510.27 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       1
Processing:     1    9   4.1      9      15
Waiting:        1    9   4.1      9      15
Total:          2    9   3.8     10      15

Percentage of the requests served within a certain time (ms)
  50%     10
  66%     11
  75%     13
  80%     14
  90%     15
  95%     15
  98%     15
  99%     15
 100%     15 (longest request)

Now we can run all different combinations of actual benchmarking tests against our own node server.
ab -n 10000 -c 100 http://127.0.0.1:8000/
ab -n 10000 -c 500 http://127.0.0.1:8000/ 
You can use apache-bench to benchmark any HTTP server as I have mentioned earlier, this is pretty helpful tool to benchmark, compare your HTTP server environments.

run  man ab  to find out other options available in Apache HTTP server benchmarking tool ab.

Happy benchmarking !!!

User groups in Linux

While performing particular task in Linux based distro one might need to verify if that user belongs to a group with right permissions.

You can verify this using two simple commands.Open your terminal and run groups,
udara@thinkPad:~$ groups
udara adm cdrom sudo dip plugdev lpadmin sambashare
This will show the groups which logged-in user belong to.

If you wish to find the groups of any other user just run, groups {user_name},
udara@thinkPad:~$ groups udara
udara : udara adm cdrom sudo dip plugdev lpadmin sambashare
udara@thinkPad:~$ groups root
root : root

Friday, October 25, 2013

MongoDB - usage of Primary Key

Like other databases MongoDB also uses primary keys to distinguish documents.
We can provide our own, otherwise MongoDB will create itself a primary key for each document. This key is a Object which consists of time-stamp and information about the machine which document created.

How to provide our own primary key

$connection = new Mongo( "10.100.0.128:27017" );
$db = $connection->selectDB("primary_key_blog");
$collection = $db->blog_collection;
$collection.insert({ _id: 1, author: "UdaraR", blog: "http://udarakr.blogspot.com" });

else we can just say,

$collection.insert({author: "UdaraR", blog: "http://udarakr.blogspot.com" });

If we read the document we stored back,
 $collection->findone( array( "author" => "UdaraR" ) );
You can see that MongoDB itself added the 12 bytes ObjectId.

The greatest thing in the second method is that you can extract created-time without storing it separately. You can simply use ObjectId.getTimestamp() method to get this done.
ObjectId("507c7f79bcf86cd7994f6c0e").getTimestamp();
Other than the above usage, as in all other databases the primary usage will be to query using the ObjectId.

$id = new MongoId("507c7f79bcf86cd7994f6c0e");
$collection->findone( array( "_id" => $id ) );
Keep in mind this is not equal to string "507c7f79bcf86cd7994f6c0e".

Other than findone, we can provide this ObjectId with find, remove, update etc.

Tried this sample on linux environment with the use of mongo driver.