La remontée d’alerte par SMS (“Short Message Service”) est un plus non négligeable dans le monitoring de systèmes d’informations critiques.
Les services gratuits permettant d’utiliser les SMS depuis le système restent rare.
Depuis plusieurs années déjà, “Google Agenda” propose à ses clients des rappels de rendez-vous par SMS. Rapidement, ce service Google fût détourné pour être utilisé comme source de remontée d’alertes (exemple : “SmsAlert : Envoyer des SMS gratuitement depuis ses serveurs” sur le site Macsim’s Mind qui utilisait le script PHP d’ Alexander Skakunov pour remonter des alertes par SMS).
Bien que très efficaces, la plupart de ces détournement sont implémentés en PHP qui n’est pas installé sur tous les serveurs.
L’idée de cet article et d’utiliser la même technique mais implémentée en BASH.
Principe de fonctionnement
Le principe est de créer un événement dans un agenda Google Calendar débutant dans 5 minutes et X secondes et d’avertir l’administrateur par SMS 5 minutes avant le début de l’événement. Le SMS sera donc envoyé après X secondes.
Reading text files is a common operation when we work with the Linux command-line. Sometimes, we know the line X in a file contains interesting data, and we want to just read line X.
In this quick tutorial, we’ll have a look at different approaches to read a specific line from a file.
2. Introduction to the Problem
The problem is pretty straightforward. Let’s get a more clear picture through an example.
For instance, we have a file called input.txt:
$ nl input.txt
1 I am line 1, I don't have any interesting data.
2 I am line 2, I don't have any interesting data.
3 I am line 3, I don't have any interesting data.
4 I am line 4, I don't have any interesting data.
5 I am line 5, interesting data: Linux is awesome!
6 I am line 6, I don't have any interesting data.
7 I am line 7, I don't have any interesting data.
As the output above shows, we’ve used the nl command to print the file’s content with line numbers.
We know that the input.txt file contains some interesting information in the fifth line. Therefore, we want to read line five only.
An alias is nothing but the shortcut to commands. The alias command allows the user to launch any command or group of commands (including options and filenames) by entering a single word. Use alias command to display a list of all defined aliases. You can add user-defined aliases to ~/.bashrcfile. You can cut down typing time with these aliases, work smartly, and increase productivity at the command prompt.
More about aliases
The general syntax for the alias command for the bash shell is as follows:
Task: List aliases
Type the following command:
alias
Sample outputs:
alias ..='cd ..'
alias amazonbackup='s3backup'
alias apt-get='sudo apt-get'
...
By default alias command shows a list of aliases that are defined for the current user.
The wc command shows the number of lines, words, and bytes contained in file. The syntax is as follows to get the file size: wc -c /path/to/file wc -c /etc/passwd Sample outputs:
5253 /etc/passwd
You can easily extract the first field either using the cut or awk command: wc -c /etc/passwd | awk '{print $1}' Sample outputs:
How to get the size of a file in a bash script using stat command
The stat command shows information about the file. The syntax is as follows to get the file size on GNU/Linux stat: stat -c %s "/etc/passwd" OR stat --format=%s "/etc/passwd" To assign this size to a bash variable:
myfilesize=$(stat--format=%s "/etc/passwd")echo"$myfilesize"## or ##myFileSizeCheck=$(stat-c%s "/etc/resolv.conf")printf"My file size = %d\n"$myFileSizeCheck
The syntax is as follows to get the file size on BSD/MacOS stat: stat -f %z "/etc/passwd" Please note that if the file is symlink you will get size of that link only with the stat command.