Showing posts with label LINUX. Show all posts
Showing posts with label LINUX. Show all posts

Wednesday, September 28, 2011

SNMP MIBs Extentions and how to use it in Linux

Hello Everyone today we will blabber for a short time about SNMP.
 I don't  now if any you are familiar with snmp configuration directives but what is very interesting for me at least is  extend, and one of the most interesting thing about that directive  that you can define your own script to be executed and assign the output to your variable name in nsExtendOutput1Line which we can use later on to create more convenient graphs using MRTG or RRD related to our needs :)  


maybe my words sounded cryptic but let me show you how this thing is being done

lets create a script to get Packet sent/recieve and both per second from one interface such as eth0 every five minutes: below is the script

#!/bin/bash


stats=$(sar -n DEV -s $(date --date='5 mins ago' +%T) | grep eth0 | tail -n2 |head -n1)

case $1 in
    received )   echo $stats | awk ' { print $6 } ';;
    sent)        echo $stats | awk ' { print $7 } ';;
    both)        echo $stats | awk ' { print $6 + $7 } ';;
esac

save it any where and make it executable . then lets go to update snmp.conf a little by add the following :


extend networktraffic /etc/snmp/myscript.sh both
extend networktrafficrcvd /etc/snmp/myscript.sh received
extend networktrafficsent /etc/snmp/myscript.sh sent

now lets do snmpwalk and check the output by executing below command if you are using snmp v3


snmpwalk -v3 -u testuser2 -l authNoPriv -a MD5 -A "pass" localhost .1|grep networktraffic

or by excuting below command if you'r configuration is
snmpwalk -v 2c -c public localhost .1|grep networktraffic

and the output will look something like this: note the ones which is marked by red


HOST-RESOURCES-MIB::hrSWRunParameters.12938 = STRING: "networktraffic"
NET-SNMP-EXTEND-MIB::nsExtendCommand."networktraffic" = STRING: /etc/snmp/myscript.sh
NET-SNMP-EXTEND-MIB::nsExtendCommand."networktrafficrcvd" = STRING: /etc/snmp//myscript.sh
NET-SNMP-EXTEND-MIB::nsExtendCommand."networktrafficsent" = STRING: /etc/snmp//myscript.sh
NET-SNMP-EXTEND-MIB::nsExtendArgs."networktraffic" = STRING: both
NET-SNMP-EXTEND-MIB::nsExtendArgs."networktrafficrcvd" = STRING: received
NET-SNMP-EXTEND-MIB::nsExtendArgs."networktrafficsent" = STRING: sent
NET-SNMP-EXTEND-MIB::nsExtendInput."networktraffic" = STRING:
NET-SNMP-EXTEND-MIB::nsExtendInput."networktrafficrcvd" = STRING:
NET-SNMP-EXTEND-MIB::nsExtendInput."networktrafficsent" = STRING:
NET-SNMP-EXTEND-MIB::nsExtendCacheTime."networktraffic" = INTEGER: 5
NET-SNMP-EXTEND-MIB::nsExtendCacheTime."networktrafficrcvd" = INTEGER: 5
NET-SNMP-EXTEND-MIB::nsExtendCacheTime."networktrafficsent" = INTEGER: 5
NET-SNMP-EXTEND-MIB::nsExtendExecType."networktraffic" = INTEGER: exec(1)
NET-SNMP-EXTEND-MIB::nsExtendExecType."networktrafficrcvd" = INTEGER: exec(1)
NET-SNMP-EXTEND-MIB::nsExtendExecType."networktrafficsent" = INTEGER: exec(1)
NET-SNMP-EXTEND-MIB::nsExtendRunType."networktraffic" = INTEGER: run-on-read(1)
NET-SNMP-EXTEND-MIB::nsExtendRunType."networktrafficrcvd" = INTEGER: run-on-read(1)
NET-SNMP-EXTEND-MIB::nsExtendRunType."networktrafficsent" = INTEGER: run-on-read(1)
NET-SNMP-EXTEND-MIB::nsExtendStorage."networktraffic" = INTEGER: permanent(4)
NET-SNMP-EXTEND-MIB::nsExtendStorage."networktrafficrcvd" = INTEGER: permanent(4)
NET-SNMP-EXTEND-MIB::nsExtendStorage."networktrafficsent" = INTEGER: permanent(4)
NET-SNMP-EXTEND-MIB::nsExtendStatus."networktraffic" = INTEGER: active(1)
NET-SNMP-EXTEND-MIB::nsExtendStatus."networktrafficrcvd" = INTEGER: active(1)
NET-SNMP-EXTEND-MIB::nsExtendStatus."networktrafficsent" = INTEGER: active(1)
NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."networktraffic" = STRING: 12145.6
NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."networktrafficrcvd" = STRING: 6309.75
NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."networktrafficsent" = STRING: 5835.86
NET-SNMP-EXTEND-MIB::nsExtendOutputFull."networktraffic" = STRING: 12145.6
NET-SNMP-EXTEND-MIB::nsExtendOutputFull."networktrafficrcvd" = STRING: 6309.75
NET-SNMP-EXTEND-MIB::nsExtendOutputFull."networktrafficsent" = STRING: 5835.86
NET-SNMP-EXTEND-MIB::nsExtendOutNumLines."networktraffic" = INTEGER: 1
NET-SNMP-EXTEND-MIB::nsExtendOutNumLines."networktrafficrcvd" = INTEGER: 1
NET-SNMP-EXTEND-MIB::nsExtendOutNumLines."networktrafficsent" = INTEGER: 1
NET-SNMP-EXTEND-MIB::nsExtendResult."networktraffic" = INTEGER: 0
NET-SNMP-EXTEND-MIB::nsExtendResult."networktrafficrcvd" = INTEGER: 0
NET-SNMP-EXTEND-MIB::nsExtendResult."networktrafficsent" = INTEGER: 0
NET-SNMP-EXTEND-MIB::nsExtendOutLine."networktraffic".1 = STRING: 12145.6
NET-SNMP-EXTEND-MIB::nsExtendOutLine."networktrafficrcvd".1 = STRING: 6309.75
NET-SNMP-EXTEND-MIB::nsExtendOutLine."networktrafficsent".1 = STRING: 5835.86








Wednesday, September 7, 2011

DHCP and BIND dynamic updates

i want to share with you how to configure DHCP dynamic updates .
first you need to create key , either by using rndc-confgen or dnssec-keygen , it will give you the same results
you need only the following attributes form the result : algorithm and secret ,
in the dhcp server edit /etc/dhcpd.conf  and add the following

in global config add :


ddns-update-style interim;
ddns-updates on;
ddns-ttl 1800;
ignore client-updates;


key "keyname" {
       algorithm hmac-md5;
       secret "T7AUeDuMo7ZadiUz1VRB1w==";
};

in scope configuration add the following


option domain-name "xxx.yyy";
option domain-name-servers 1.1.1.1,2.2.2.2;
option ntp-servers 10.1.46.50;
zone xxx.yyy. { primary 127.0.0.1; key keyname; }
zone 1.1.1.in-addr.arpa. { primary 127.0.0.1;key  keyname; }

and from the DNS part add the following to named.conf in the global configuration area, taking into consideration that it is the same key has been added to DHCP configuration


key "keyname" {
        algorithm hmac-md5;
        secret "T7AUeDuMo7ZadiUz1VRB1w==";
};

and for every zone you wish the DHCP server to update add the following
allow-update { key keyname; };

Notes :

1. if the DHCP and BIND located in the same server. you better allow update only through loopback interface
2. dhcpd user should have a write access to zones directory and it could be done by using setfacl

NTP client for Linux VM on ESX server

i had a problem long time ago with Linux VMs on ESX host, and one of the problems is NTP sync

so the solution for who he still has the issue is the following
edit /boot/grub/menu.lst or /boot/grub/grub.conf and append the following to the kernel line:clocksource=acpi_pm clock=pits 
and it will work like charm .

SSH connectivity library

Hello again

this library has been written to help accessing Unix\ Linux servers with private key. it needs  ganymed-ssh2-build210.jar to work


package lib;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class SSHLib
{
       static String sshPrivateKey;
static Connection conn = null;
static Session session = null;
       static String sshUsername;
String output ="";


       public SSHLib() throws IOException
{
init();
}
 
       private void init() throws IOException{
char privateKeyChar[] = sshPrivateKey.toCharArray();

conn = new Connection(ftpHostname,22);
conn.connect();

boolean isAuthenticated = conn.authenticateWithPublicKey(sshUsername, privateKeyChar, null);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");

session = conn.openSession();
}
       
        public String excuteCMD(String cmd) throws IOException
{
session = conn.openSession();
session.execCommand(cmd);
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

while (true)
{

String line = br.readLine();
if (line == null)
break;
else
output+=line;
}

return output;
}

public Integer getExitStatus()
{
return session.getExitStatus();
}

        public void disconnect()
{
session.close();
conn.close();
}

        public String getSize(String ftpAccount , String path) throws IOException {

return excuteCMD("du -sh " + path + "|" + "awk '{print $1}'" );
}

        public void deleteFolder(String path) throws IOException{

excuteCMD("rm -rf " + path);
}

       public static void main(String[] args) throws IOException {

SSHLib ssh = new SSHLib();
Integer x = ssh.getExitStatus();
//System.out.println("exit status :" + x);
System.out.println(ssh.getSize(null, "/home/"));
ssh.disconnect();
}
       

}