Howtos:CreateAgentPlugin

From OCS Inventory NG
(Redirected from Howtos:CreateAngetPlugin)
Jump to: navigation, search

How To Create a plugin

(this information belongs to OCS NG V1)

I Wanted to see netstat information of my hosts,so i developed this plugin, but you adapt and make you own.

This is The information of netstat:

 Active Internet connections (servers and established)
 Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
 tcp        0      0 127.0.0.1:199               0.0.0.0:*                   LISTEN      1380/snmpd          
 tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      336/mysqld          
 tcp        0      0 0.0.0.0:9102                0.0.0.0:*                   LISTEN      1488/bacula-fd       
 tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1428/sendmail: acce 
 tcp        0      0 193.137.196.16:3306         193.136.44.187:18578        ESTABLISHED 336/mysqld          
 tcp        0      0 193.137.196.16:3306         193.136.44.187:18577        ESTABLISHED 336/mysqld          
 tcp        0      0 :::80                       :::*                        LISTEN      6163/httpd          
 tcp        0      0 :::22                       :::*                        LISTEN      16029/sshd          

1. Create the plugin in : /usr/lib/perl5/vendor_perl/5.8.8/Ocsinventory/Agent/Backend/netstat.pm 2. The Plugin must be writen in perl 3. The plugin :

 #!/usr/bin/perl
 package Ocsinventory::Agent::Backend::netstat;
 #use strict;
 #use warnings;
 sub check {
       return 1;
 }
 sub run ($){
       my $params = shift;
       my $inventory = $params->{inventory};
       my @outputs = `netstat -ntaulpe | grep LISTEN`;
       foreach my $output (@outputs)
       {
               my @data = split(' ', $output); # Divide Netstat
               $Proto        =  $data[0]; # Proto
               $LocalAddress =  $data[3]; # LocalAddress
               $State        =  $data[5]; # State
               $User         =  $data[6]; # User
               $Inode        =  $data[7]; # Inode
               $Pid          =  $data[8]; # Pid/Program
               $inventory->addNetstat({
               'PROTO'          => $Proto,
               'LOCALADDRESS'   => $LocalAddress,
               'STATE'          => $State,
               'USER'           => $User,
               'INODE'          => $Inode,
               'PID'            => $Pid,
               });
       }
}
1;

Note : that the field that i want in the ocs database are on this

    $inventory->addNetstat({
               'PROTO'          => $Proto,
               'LOCALADDRESS'   => $LocalAddress,
               'STATE'          => $State,
               'USER'           => $User,
               'INODE'          => $Inode,
               'PID'            => $Pid,
               });

4. Next Edit /usr/lib/perl5/vendor_perl/5.8.8/Ocsinventory/Agent/XML/Inventory.pm Add this :

=item addNetstat()
Add a NetStat in the inventory.
=cut
sub addNetstat {
 my ($self, $args) = @_;
 my $logger = $self->{logger};
my $proto = $args->{PROTO};
 my $localaddress = $args->{LOCALADDRESS};
 my $state = $args->{STATE};
 my $user  = $args->{USER};
 my $inode = $args->{INODE};
 my $pid   = $args->{PID};
 push @{$self->{h}{CONTENT}{NETSTAT}},
 {
   PROTO => [$proto?$proto:],
   LOCALADDRESS => [$localaddress?$localaddress:],
   STATE => [$state?$state:],
   USER => [$user?$user:],
   INODE => [$inode?$inode:],
   PID => [$pid?$pid:],
 };
}

you may want to add an entry to the checksum structure/hash, if you want to use a checksum:

=cut
sub processChecksum {
 my $self = shift;

 my $logger = $self->{logger};
#To apply to $checksum with an OR
 my %mask = (
   'HARDWARE'      => 1,
   'BIOS'          => 2,
   'MEMORIES'      => 4,
   'SLOTS'         => 8,
   'REGISTRY'      => 16,
   'CONTROLLERS'   => 32,
   'MONITORS'      => 64,
   'PORTS'         => 128,
   'STORAGES'      => 256,
   'DRIVES'        => 512,
   'INPUT'         => 1024,
   'MODEMS'        => 2048,
   'NETWORKS'      => 4096,
   'PRINTERS'      => 8192,
   'SOUNDS'        => 16384,
   'VIDEOS'        => 32768,
   'SOFTWARES'     => 65536,
   'VIRTUALMACHINES' => 131072,
   'NETSTAT'       => 262144,
 );


5. Run this command ocsinventory-agent --debug --local /tmp and search for <NETSTAT> XML tag, if it appear it's all okay.

 <NETSTAT>
     <LOCALADDRESS>:::22</LOCALADDRESS>
     <PROTO>tcp</PROTO>
     <STATE>LISTEN</STATE>
 </NETSTAT>


6. Create the Database Table

CREATE TABLE `ocsweb`.`netstat` (
`ID` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`HARDWARE_ID` INT( 11 ) NOT NULL ,
`PROTO` TEXT NOT NULL ,
`LOCALADDRESS` VARCHAR( 50 ) NOT NULL ,
`STATE` VARCHAR( 50 ) NOT NULL ,
`USER` VARCHAR( 10 ) NOT NULL ,
`INODE` VARCHAR( 50 ) NOT NULL ,
`PID` VARCHAR( 50 ) NOT NULL 

7. Next Edit [root@ocs01 OS]# vi /usr/lib/perl5/vendor_perl/5.8.8/Apache/Ocsinventory Map.pm

and add this :

netstat => {
  mask => 262144,
  multi => 1,
  auto => 1,
  delOnReplace => 1,
  sortBy => 'HARDWARE_ID',
  writeDiff => 0,
  cache => 0,
  fields =>  {
     PROTO => {},
     LOCALADDRESS => {},
     STATE => {},
     USER => {},
     INODE => {},
     PID => {},
  }
 },

note: setting the mask on the server side requires a corresponding setting on the client side, see 4.

8. Restart the Apache, and all is done, if do i select * to netstat you will see, all data.

Next will be adding GUI INFORMATION

As you can see this can do to all kind of things.

Pedro Oliveira Portugal FCCN - Fundação para a Computação Científica Nacional

http://www.newitperson.com