miércoles, 15 de abril de 2020

Graphviz. Simple solution for recreating graphics

This is the first article that I publish in English, the main reason to do it is because all the documentation related to context maps that I have found so far, are related in English and obviously my way to contribute to the extensive Internet community, is to do it in a universal language, at least for this article.

Well, today I want to talk about two utilities that have helped me a lot, one of them is graphviz.org. Graphviz, which has versions for Windows, Mac or Linux (in my case I have tried in Linux and Mac OS), is a powerful Open Source tool that allows to easily generate almost any graphic, cool stuff like this:



Or simpler things than recreating a binary tree that I explained in the previous section, https://www.larebelion.com/2020/04/arboles-binarios.html

Imagine that in code C, you want to represent the resulting binary tree, with few lines of code you could get something like this:


Let me a simple C function to add to the previous tree and generate this:

static void _generating_graphviz_details(const tree_t node, const void *extra)
{
 FILE *f = (FILE *) extra;

 fprintf(f, " node_%p [label=\"{%p | content: %d | { <izqda> %p | <dcha> %p}\"];\n",
  node, node, node->content, node->left, node->right
 );
 if (node->left != NULL) {
  fprintf(f, " node_%p:left -> node_%p;\n", node, node->left);
 }
 if (node->right != NULL) {
  fprintf(f, " node_%p:dcha -> node_%p;\n", node, node->dcha);
 }
 fprintf(f, "f");
}

Okay, hopefully this tiny contribution will help you in the dynamic generation of your graphics.

martes, 14 de abril de 2020

Backup de seguridad de tu máquina Linux

En estos días de confinamiento, me he decidido a ordenar un poco mis máquinas, en mi caso, cuento con tres raspberry en casa con diferentes distribuciones de Linux que utilizo para montar labs de sistemas, de programación, bases de datos como MariaDB o PostgreSQL e incluso cosas de seguridad o hardening.

Las raspberry, por la experiencia que tengo ya con ellas de varios años pueden sufrir problemas con la tarjeta SD y dejarte tirado. Como me ha pasado ya varias veces, me he decidido por escribir un script en shell para salvaguardar la información. Hasta aquí, todo más o menos sencillo, el problema es... ¿donde lo almaceno?.

Gracias a mi hermano, he descubierto http://mega.nz, se trata de una nueva startup del reputado empresario Kim_Dotcom, el creador de Megaupload. Lo primero que recomiendo hacer es crearte una cuenta gratuita de Meganz (50GB gratis).



Una vez la crees, descargate el componente para utilizar via json la integración con el API de meganz, el componente se llama Megacmd


Una vez que ya tengas todo esto instalado en tu máquina, ya puedes personalizar este pequeño shell script y almacenar tus backups en la nube.



#!/bin/bash

NAME=`hostname`
LIMITSIZE=5485760 # 5G = 10*1024*1024k   # limit size in GB   (FLOOR QUOTA)
MAILCMD=mail
R=/home/xxxx/src
LOG=$R/log/log-$(basename ${0}).log
EMAILIDS="user@domain.net"
MAILMESSAGE=$R/log/tmp-$(basename ${0})

if [ -d /media/4TB ]
then
        ROOTPATH=/media/4TB/servers/$NAME  # in case we've mount volume
        MOUNTP=/media/4TB
else
        ROOTPATH=/opt/$NAME  # in case no
        MOUNTP=/
fi

BKDIR=$ROOTPATH/bkdir # simple text file for to list your directories for backup, normally /etc /home/user /root /var/www/html ...
BKPATH=$ROOTPATH/backup
FREE=$(df -k --output=avail "$MOUNTP" | tail -n1) # df -k not df -h because is more exact

if [ ! -d $BKPATH ]
then
        mkdir -p $BKPATH
fi

if [ ! -d $R/log ]
then
        mkdir -p $R/log
fi

email_on_failure () # functions for launch email alert
{

          sMess="$1"
          echo "" >$MAILMESSAGE
          echo "Hostname: $(hostname)" >>$MAILMESSAGE
          echo "Date & Time: $(date)" >>$MAILMESSAGE

          # Email letter formation here:
          echo -e "\n[ $(date +%Y%m%d_%H%M%S%Z) ] Current Status:\n\n" >>$MAILMESSAGE
          cat $sMess >>$MAILMESSAGE

          echo "" >>$MAILMESSAGE
          echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
          echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE

          # sending email (need to have an email client set up or sendmail)
          $MAILCMD -s "Urgent MAIL Alert For $(hostname) Server" "$EMAILIDS" < $MAILMESSAGE

          [ -f $MAILMESSAGE ] && rm -f $MAILMESSAGE
}

if [ "$FREE" -lt "$LIMITSIZE" ]
then
          echo "Writing to $LOG"
          echo "MAIL ERROR: Less than $((($FREE/1000))) MB free (QUOTA) on $MOUNTP!" | tee ${LOG}
          echo -e "\nPotential Files To Delete:" | tee -a ${LOG}
          find $MOUNTP -xdev -type f -size +500M -exec du -sh {} ';' | sort -rh | head -n20 | tee -a ${LOG}
          email_on_failure ${LOG}
else
   echo "Currently $((($FREE-$LIMITSIZE)/1000))) MB of QUOTA available of on $MOUNTP. " 
fi

if [ ! -f $BKDIR ]
then
        echo "Doesn't exist bkdir file. Aborting!" 
        exit 1
else
        filename=$NAME"_daily_backup_""$(date +'%Y_%m_%d')".tar.gz
        logfile="$R/"log/backup_log_"$(date +'%Y_%m')".log

        # Packages installed on Linux

        dpkg-query -W -f='${Installed-Size} ${Package}\n' | sort -n > $BKPATH/packages_installed.txt

        # Compressing my directories

        tar czf $BKPATH/$filename -T $BKDIR
        echo "dumping source code finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"

        # Changing permission

        chown root -R "$BKPATH"
        chown root "$logfile"
        chmod 644 $BKDIR
        chmod 600 $ROOTPATH

        echo "file permission changed" >> "$logfile"

        # removing old files and temporary files

        find "$BKPATH" -name "*daily_backup*" -mtime +3 -exec rm {} \;
        echo "old files deleted" >> "$logfile"
        echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"

        # pushing mega.nz

        mega-login user@domain.net password
        mega-sync $BKPATH $NAME
        echo "pushing backup to mega.nz $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
        echo "*****************" >> "$logfile"
        exit 0
fi

domingo, 12 de abril de 2020

Árboles binarios

Ejercicio sencillo para representar un Arbol Binario, funciones básicas:

  • Crear nuevo árbol
  • Insertar hojas
  • Destruir Árbol
  • Imprimir en (Preorden, Postorden e Inorden)
  • Localizar nodos menores a un número, mostrarlos y contarlos.



#include <stdio.h>
#include <stdlib.h>

typedef struct nodo
{
    struct nodo *hizqdo;
    struct nodo *hdrcho;
    int contenido;
} nodo;

typedef struct nodo *arbol;

arbol nuevo_arbol (int contenido);
void destruir_arbol (arbol a);
void insertar_arbol (arbol nuevo, arbol *a);
void encontrar_menores (arbol a, int comparador, int *contador);
void imprimir (arbol a, int modo);

int main (void)
{
    int contenidos[] = {1, 20, 43, 12, 45, 21, 75, 26, 16, 74, 74, 36, 24, 2, 29, 97, -1};
    int *c = contenidos;
    arbol a = NULL;
    int contador = 0;
    while (*c >= 0) {
        arbol nuevo = nuevo_arbol(*c);
        insertar_arbol (nuevo, &a);
        c++;
    }

    printf ("\nPreorden:\n");
    imprimir(a, 1);
    printf ("\nPostorden:\n");
    imprimir(a, 2);
    printf ("\nInorden:\n");
    imprimir(a, 3);

    printf ("\nEncontrar menores de 50:\n");
    encontrar_menores(a, 50, &contador);
    printf ("\nContador: %d", contador);

    destruir_arbol(a);
    return EXIT_SUCCESS;
}

arbol nuevo_arbol (int contenido)
{
    arbol ret;
    if ((ret = calloc(1,sizeof(nodo))) == NULL) {
        return NULL;
    }

    ret->contenido = contenido;

    return ret;
}

void destruir_arbol (arbol a)
{
    if (a == NULL) {
        return;
    }
    destruir_arbol(a->hizqdo);
    destruir_arbol(a->hdrcho);
    free(a);
}

void insertar_arbol (arbol nuevo, arbol *a)
{
    if (*a == NULL) {
        *a = nuevo;
        return;
    }

    if (nuevo->contenido < (*a)->contenido) {
        insertar_arbol(nuevo, &((*a)->hizqdo));
    } else if (nuevo->contenido > (*a)->contenido) {
        insertar_arbol(nuevo, &((*a)->hdrcho));
    }
}

void encontrar_menores (arbol a, int comparador, int *contador)
{
    if (a == NULL) {
        return;
    }

    if (a->contenido < comparador) {
        printf ("%d | ", a->contenido);  
        ++(*contador);
    } 
    encontrar_menores (a->hizqdo, comparador, contador);
    encontrar_menores (a->hdrcho, comparador, contador);
}

void imprimir (arbol a, int modo) 
{
    if (a == NULL) {
        return;
    }

    if (modo == 1) { // Preorden
    printf ("%d | ", a->contenido);
    imprimir(a->hizqdo, 1);
    imprimir(a->hdrcho, 1);
    } else if (modo == 2){ // Postorden
        imprimir(a->hizqdo, 2);
        imprimir(a->hdrcho, 2);
        printf ("%d | ", a->contenido);
        } else if (modo == 3) {// Inorden
            imprimir(a->hizqdo, 3);
            printf ("%d | ", a->contenido);
            imprimir(a->hdrcho, 3);
            }
}

miércoles, 8 de abril de 2020

Matrices dinámicas en C

A continuación un ejercicio, similar a otros que he publicado, pero ésta vez con Matrices de dimensiones dinámicas.


#include <stdio.h>
#include <stdlib.h>


int ** IntroduceMatriz (int *, int *);
void SumaFilas (int **, int, int);
void MultiplicaColumnas (int **, int, int);
void RotaMatrizDerecha (int **, int, int);
void MostrarMatriz (int **, int, int);

int main () 
{
    int **matriz, f ,c;
    int i;

    matriz = IntroduceMatriz(&f, &c);
    MostrarMatriz(matriz,f,c);
    SumaFilas(matriz,f,c);
    MultiplicaColumnas(matriz,f,c);
    RotaMatrizDerecha(matriz,f,c);
    MostrarMatriz(matriz,f,c);
    for (i=0; i<f ; i++) {
        free(matriz[i]);
    }
    free(matriz);
    return EXIT_SUCCESS;
}

int ** IntroduceMatriz (int *f, int *c)
{
    int **matriz;
    int i, j;

    printf ("Indique numero de filas: \n");
    scanf ("%d",f);
    fflush (stdin);  // Limpiar el buffer de entrada
    printf ("Indique numero de columnas: \n");
    scanf ("%d",c);
    fflush (stdin);

    matriz = (int **) calloc (*f,sizeof(int*));

    for (i=0 ; i<*f ; i++) {
            matriz[i] = (int*) calloc (*c,sizeof(int));
    }

    for (i=0 ; i<*f ; i++) {
        for (j=0 ; j<*c ; j++) {
            printf ("\nIntroduzca posición %d - %d: ",i,j);
            scanf("%d",&matriz[i][j]);
            fflush(stdin);
        }
    }
    return matriz;
}

void MostrarMatriz (int **matriz, int f, int c)
{
    int i,j;
    for (i=0 ; i<f ; i++) {
        for (j=0 ; j<c ; j++) {
            printf ("%d ",matriz[i][j]);
        } printf ("\n");
    }
}

void SumaFilas (int **matriz, int f, int c)
{
    int i,j,suma;
    for (i=0 ; i<f ; i++) {
        suma = 0;
        for (j=0 ; j<c ; j++) {
            suma += matriz[i][j];
        } printf ("La fila %d suma: %d \n",f,suma);
    }
}

void MultiplicaColumnas (int **matriz, int f, int c)
{
    int i,j, multiplica;
    for (j=0 ; j<c ; j++) {
        multiplica = 1;
        for (i=0 ; i<f ; i++) {
            multiplica *= matriz[i][j];
        } printf ("La columna %d da: %d \n",j,multiplica);
    }
}

void RotaMatrizDerecha (int **matriz, int f, int c)
{
    int i,j;
    int **matrizt;

 
    matrizt = (int**) calloc (f,sizeof(int*));
    for (i=0; i<f ; i++) {
        matrizt[i] = (int*) calloc (c,sizeof(int));
    }

    for (i=0 ; i<f ; i++) {
        for (j=0 ; j<c ; j++) {
            if (j == c-1) {
                matrizt[i][0] = matriz[i][j]; 
            } else {
                matrizt[i][j+1] = matriz[i][j];
            }     
        } 
    }

    for (i=0 ; i<f ; i++) {
        for (j=0 ; j<c ; j++) {
            matriz[i][j] = matrizt[i][j];
        } 
    }

}