Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

Analisar log httpd (Personalizado).

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    C / C++ Analisar log httpd (Personalizado).

    Analisar log httpd (Personalizado)

    Olá a todos quero compartilhar com vocês o código fonte de um analisador de logs Httpd.

    Código:
    Analisar log httpd (Personalizado)	
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct tnode {
     char *string;
     int count;
     struct tnode *left, *right;
    };
    
    /* Número total de nós */
    static int tnodecount = 0;
    
    struct tnode *addtree(struct tnode *, char *);
    void freetree(struct tnode *);
    void freetarr(struct tnode **);
    char *strdup(const char *s);
    int tree_to_array(struct tnode **, struct tnode *); 
    int cmpr(const void *x, const void *y); 
    
    int main(void) {
     FILE *fp = stdin;
     struct tnode *root = {0};
     struct tnode **tnarray = {0};
     char line[1024];
     char *start = NULL;
     char *end   = NULL;
     char *filename = NULL;
     int i = 0;
    
    
     while(fgets(line, 1024, fp) != NULL) {
      if((start = strchr(line, '"')) == NULL)
       continue;
    
      start++;
      for(end = start; *end; end++) {
       if(*end == '"')
        break;
      }
      *end = '\0';
      if((filename = strchr(start, ' ')) == NULL)
       continue;
    
      filename += 2;
      end = strrchr(filename, ' ');
      *end = '\0';
    
      /* Cresce uma árvore */
      root = addtree(root, filename);
     }
    
     /* aloca a memória para o array ptr */
     tnarray = malloc(tnodecount * sizeof(*tnarray));
     /* ler btree em array */
     tree_to_array(tnarray, root); 
     /* matriz qsort */
     qsort(tnarray, tnodecount, sizeof(*tnarray), cmpr);
    
     /* Imprimi o resultado */
     for(i = 0; i < tnodecount; i++)
      printf("%4d %s\n", tnarray[i]->count, tnarray[i]->string);
    
     /* limpa a bagunça. */
     freetree(root);
     freetarr(tnarray);
     fclose(fp);
    
     return 0;
    }
    
    struct tnode *addtree(struct tnode *p, char *w) {
     int cond;
    
     if(p == NULL) {
      p = (struct tnode *)malloc(sizeof(struct tnode));
      p->string = strdup(w);
      p->count = 1;
      p->left = p->right = NULL;
      tnodecount++;
     } else if((cond = strcmp(w, p->string)) == 0)
      p->count++;
     else if(cond < 0)
      p->left = addtree(p->left, w);
     else
      p->right = addtree(p->right, w);
    
     return p;
    }
    
    void freetree(struct tnode *p) {
     if(p != NULL) {
      freetree(p->left);
      freetree(p->right);
      free(p->string);
      free(p);
     }
    }
    
    void freetarr(struct tnode **p) {
     int i = 0;
    
     if(p != NULL)
      for(i = 0; i < tnodecount; i++)
       free(p);
    }
    
    char *strdup(const char *s) {
     char *result = malloc(strlen(s) + 1);
    
     if(result == NULL)
      return NULL;
    
     strcpy(result, s);
     return result;
    }
    
    int tree_to_array(struct tnode **array, struct tnode *tree) {
     static struct tnode **write = NULL;
    
     if(tree == NULL) 
      return -1;
    
     if(array != NULL)
      write = array;
    
     if(tree != NULL) {
      *write = tree, write++;
      if(tree->left != NULL)
       tree_to_array(NULL, tree->left);
      if(tree->right != NULL)
       tree_to_array(NULL, tree->right);
     }
    
     return 0;
    }
    
    int cmpr(const void *x, const void *y) { 
     struct tnode * const *a = x;
     struct tnode * const *b = y;
     int retval = 0; 
     
     if(a == NULL || b == NULL)
      return -2;
    
     if((*a)->count > (*b)->count)
      retval = -1;
     else if((*a)->count < (*b)->count)
      retval = 1;
     else
      retval = 0;
    
     return retval;
    }
    Espero que gostem.


    Abraços.


    WCG147
    sigpic

  • Font Size
    #2
    Boa maninho.

    Salvando aqui, vai ser util não só pra min, mais pra muitos do forum.

    Thanks.
    WhiteCollarGroup till I die
    MI5, MI6,NSA,FBI,Army, CIA,Navy,Air Force, Mossad, PF and all this shit can't stop me.

    Comment


    • Font Size
      #3
      Valeu brother

      Comment


      • Font Size
        #4
        Olá
        Como fazes o log?
        Pode ser do género "tcpdump port 443 -q -i eth0 -n -A > /var/log/log.txt"?

        Comment

        X
        Working...
        X