[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Log-file - Code - Java - Bash



Hello,

I've got some code to process log-files. It's seems to work, great and fast.

The working bit is coded in Java. Maybe I'll port it to C++.

I've got some trouble running en testing it in my Cygwin-environment.

Would you like to have it?

Well, if you do, it's yours!

The source is attached to this email.

Kind regards,

Ensio M.V. Puranen
Stats - A Utility to monitor a Linux Server

Opensource-d with any licence.

Compile the java-code! Configure the bash-glue. Let it run with a Cron-Job, at set times.

ensio@1000101.nl - Ensio M.V. Puranen

----
Library for EmailApp - Place it in the path /lib
https://www.oracle.com/java/technologies/javamail-releases.html

#!/bin/bash
# Author Ensio M.V. Puranen
# ensio@1000101.nl
#

PAD="/home/stats";

cd $PAD;

DATUM=`date +"%m-%d-%y"`
SERVER_NAME="www.anyname.nl"
EMAIL_APP="EmailApp.jar"

#EMAIL DATA
EMAIL_ADDRESSES=("ensio@1000101.nl");
EMAIL_SUBJECT="Logs $SERVER_NAME - $DATUM"
EMAIL_RETOUR="info@anyname.nl"


# Run the Stats script
$PAD/stats.sh > $PAD/$SERVER_NAME-$DATUM.txt

pwd;
for i in "${EMAIL_ADDRESSES[@]}"
do
echo Email voor $i;
java -jar $EMAIL_APP $i  "$EMAIL_SUBJECT" "Logs :: $SERVER_NAME"  $PAD/$SERVER_NAME-$DATUM\.txt  $EMAIL_RETOUR
done;

# Remove the file
rm -f $PAD/$SERVER_NAME-$DATUM\.txt





#!/bin/bash
# stats.sh
# A script that produces a string that reflects your system on 
# a certain day and time-frame.
#
# author Ensio M.V. Puranen
#
# ensio@1000101.nl
#

nameofserver="Hmmmmm.com"

# Define the date's of the log-files.
# They could be different in different systems.
# There is a utility for it.

somedatum=$(date "+%b %_d")
apachedatum=$(date +'%d/%b/%Y')
mysqldatum=$(date +'%Y-%m-%d')


# The Java App
# It processes al IP's, sum's them en resolves the IP's,
# display's the list.

the_app="/home/stats/iplogger.jar"

linex="-----------------------------------------------------"

# Some build in Utilities
# Make sure you have them.
echo $linex
echo Rapport: `date` $nameofserver
echo $linex
echo

echo $linex
echo Uptime:
echo $linex
uptime
echo $linex
echo

echo $linex
echo Diskspace
echo $linex
df
echo $linex
echo

echo $linex
echo Traffic
echo $linex
/sbin/ifconfig
echo $linex
echo

echo $linex
echo Memory usage
echo $linex
free
echo $linex
echo


## Great Utility!
#  Sysstat package
echo $linex
echo Systemload
echo $linex
sar
echo $linex
echo

echo $linex
echo Lastlog
echo $linex
lastlog
echo $linex
echo

echo $linex
echo All logins:
echo $linex
last
echo $linex
echo


# Use the fasted grep you can find and use it as a Filter.
# Glue the pipes...
#
# You can do this with any log-file. You can repeat the pattern below.
#
cd /var/log
echo $linex
echo All SSH-Logins:
echo $linex
grep -R "$somedatum" auth.log  | grep ssh |  java -jar $the_app
echo $linex
echo

cd /var/log/apache2
echo $linex
echo Apache Access Log:
echo $linex
grep -R "$apachedatum" access.log  | java -jar $the_app
echo $linex
echo

echo $linex
echo Apache Error Log:
echo $linex
grep -R "$apachedatum" error* | java -jar $the_app
echo $linex
echo

echo $linex
echo Mysql Error Log
echo $linex
grep -R "$mysqldatum" /var/log/mysql/error.log | java -jar $the_app
echo $linex
echo

echo $linex
echo Processes on the Server
echo $linex
ps -e
echo $linex
echo
package emailapp;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Transport;

/**
 * Just an App to send an email with a attachment from the CLI.
 * By default it uses the SMTP-service of Localhost.
 * 
 * 26.07.2015 - Did something. Downloaded a bit of it from somewhere...
 * 24.02.2023 - Translated it to (propper) English.
 *
 * @author Ensio M.V. Puranen
 * 
 * ensio@1000101.nl
 * 
 */
public class EmailApp {
    
    public static void main(String[] args) {
        if( args.length == 5  ) {                  
            String emailadres   = args[0];
            String topic        = args[1];
            String msgx      	= args[2];
            String attachement  = args[3];
            String origin       = args[4];
 
            Properties props = new Properties();
            props.put("mail.smtp.host", "127.0.0.1");
            Session session = Session.getInstance(props, null);
            
            try { 
            
                Message msg = new MimeMessage(session);

                msg.setFrom(new InternetAddress(origin));
                InternetAddress[] toAddresses = { new InternetAddress(emailadres) };
                msg.setRecipients(Message.RecipientType.TO, toAddresses);
                msg.setSubject(topic);
                msg.setSentDate(new Date());

                // creates message part
                MimeBodyPart messageBodyPart = new MimeBodyPart();
                messageBodyPart.setContent(msgx, "text/html");

                // creates multi-part
                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(messageBodyPart);

                // adds attachments
                if (attachement != null && attachement.length() > 0) {
                        MimeBodyPart attachPart = new MimeBodyPart();
                        try {
                            attachPart.attachFile(attachement);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }

                        multipart.addBodyPart(attachPart);
                    }
                msg.setContent(multipart);
                Transport.send(msg);
        }
        catch ( Exception ex ) {
            System.out.println( ex.getMessage() );
        }
       
     } else {
            System.out.println( "EmailApp <emailadres> <topic> <msg> <attachment> <origin> ");
        }
    }
}
/**

Don't know much about assembly
Don't know much about pripietery
Don't know much about that sciencebook
Don't know much about that c-course I took

But I do know one and one isn't two...

@author Ensio M.V. Puranen

ensio@1000101.nl

*/
package iplogger;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.net.*;
import java.io.*;
import java.util.function.*;


public class IPLogger {  
    
	// To html?
    public static void main(String[] args){
        ArrayList<String> v = new ArrayList();
        ArrayList<String> list = new ArrayList();
        BufferedReader reader;   
        int total = 0;
        Pattern pattern = Pattern.compile("\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b"); 
        String z = "";
        
        //File file = new File("test.log");

        try { 
            reader = new BufferedReader(new InputStreamReader(System.in));    
            //reader = new BufferedReader(new InputStreamReader( new FileInputStream(file)));
            while( (z = reader.readLine()) != null ) {              
                v.add(z);    
            }   
            for(String s : v) {              
                Matcher matcher = pattern.matcher(s);
                while(matcher.find()) 
                    // 1, 2, 3
                    list.add(matcher.group());
            }              
        } catch ( Exception ex ) {
            System.out.println( ex.getMessage());
        }  
        
        
        Set<String> ips = new HashSet(list);
        printLine();
        System.out.printf( "%-15s \t %s \t \t %s  \n", "IP Address", "Count", "Hostname");
        printLine();
        
        ArrayList ipList = new ArrayList<IP>();
        
        for (String ip_ : ips) {
            IP ip = new IP();
            ip.ip = ip_;
            
            if( ip.isAnIp() ) {
                ip.count = Collections.frequency(list, ip_);      
                total += ip.count;
                ipList.add(ip);
            }
              
        } 
        
        Consumer<IP> doTheList = a -> a.doJob();
        ipList.parallelStream().forEach(doTheList);
        
        printLine();      
        System.out.println( "IP Addresses: \t \t "+ ips.size() ); 
        System.out.println( "Total: \t \t \t "+ total); 
        printLine();
    }
    private static void printLine() {
        System.out.println("------------------------------------------------------------------------------------");
    }
}

 
class IP {   
    int count = 0;
    String ip = "";
    String hostname = "";

    public void print() {       
        System.out.printf( "%-15s \t %s \t \t %s  \n", ip, count, hostname);

    }

    public boolean isAnIp() {
        StringTokenizer token = new StringTokenizer(ip, ".");
        if( token.countTokens() == 4 ) {
            int a = Integer.parseInt(token.nextToken());
            int b = Integer.parseInt(token.nextToken());
            int c = Integer.parseInt(token.nextToken());
            int d = Integer.parseInt(token.nextToken());
            return (inBetween(a) && inBetween(b) && inBetween(c) && inBetween(d));

        }
        return false;
    }

    private boolean inBetween(int x) {
        if( x >= 0 && x < 256 ) 
            return true;
        else 
            return false;
    }
    public void doJob() {
        hostname = nslookup(ip);
        print();
    }
    private static String nslookup( String ip ) {   
        String hostname = "";
        try {
            InetAddress inetHost = InetAddress.getByName(ip);
            hostname = inetHost.getHostName();
        } catch ( Exception ex ) {}
        return hostname;
    }
}


Reply to: