Saturday, March 5, 2016

Linux: How To Setup Email Alerts on Linux Using Gmail as SMTP

Prerequisites and assumptions

Before we get started, you’ll want to make sure that all of these conditions are met:

·         You have an SMTP server that can receive the emails from your machines and send them to the recipient (i.e. your corporate Gmail).

·         You have the credentials for a user that is able to send Email on that server (i.e. a mailbox or a Gmail account).

Setup

To install the ssmtp (Simple S.M.T.P) package, use the following command:

sudo aptitude install ssmtp

Then edit the configuration file:

#vi /etc/ssmtp/ssmtp.conf

Adjust and add as necessary the following parameters:

root=username@gmail.com

Change it from postmaster to the machines admin’s Email.

mailhub=smtp.gmail.com:587

Your mail server in our case this is Gmail so we have to specify the port as 587, for regular SMTP servers this is usually not necessary.

hostname=username@gmail.com

Usually the name of the machine is automatically filled by the package setup, if the machine has a mailbox this should be fine, but if it doesn’t or the name is not the same as the mailbox adjust accordingly.

UseSTARTTLS=YES

Enable TLS for secure session communication.

AuthUser=username

The username of the sending mailbox

AuthPass=password

The password of the sending mailbox

FromLineOverride=yes

Sends the hostname instead of root[root@hostname.FQDN].

In order to make the default (root) “from” field be the server name, edit the/etc/ssmtp/revaliases file:

#vi /etc/ssmtp/revaliases

And add into it the desired translation which in our Gmail examples case will be:

root:machine-name@some-domain.com:smtp.gmail.com

Incredibly this is all you have to do to enable the ability. From now on, the machine will Email you when something is up.

Confirming setup

Lets test that our ssmtp setup was correct by sending an Email:

#echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv your-email@some-domain.com

The “-vvv” turns on verbosity output so don’t get alarmed… this is just in case you encounter any problems, you will have some sort of output to Google for.

If all goes well, you should be getting the Email in a couple of seconds.

OEL: Install Google-Chrome on Oracle Enterprise Linux 6.2

These steps show how to install Google Chrome on Oracle Enterprise Linux 6.x.  I suspect these steps would also work with Fedora, CentOS and Red Hat (RHEL).  This approach uses Google's YUM repository.
§  Login or switch to the root user
§  Create a file named google.repo in the /etc/yum.repos.d dirctory
§  Add the appropriate segment to your new google.repo file
32 bit
[google-chrome]
name=google-chrome – 32-bit
baseurl=
http://dl.google.com/linux/chrome/rpm/stable/i386
enabled=1
gpgcheck=1
gpgkey=
https://dl-ssl.google.com/linux/linux_signing_key.pub


64 bit
[google-chrome]
name=google-chrome – 64-bit
baseurl=
http://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=
https://dl-ssl.google.com/linux/linux_signing_key.pub

#yum install google-chrome-stable

CentOS / RHEL: See Detailed History Of yum Commands

I would like to see detailed information on the history of yum transactions such as updates, deleted packages, and other information. How do I see history of yum commands on CentOS / RHEL 6.x based server? How do I undo or redo or rollback packages using yum command?

The yum command has history option on the latest version of CentOS / RHEL v6.x+. To database are normally found in /var/lib/yum/history/ directory. The history option was added at the end of 2009 (or thereabouts) to yum command. The history command allows an admin to access detailed information on the history of yum transactions that have been run on a system. You can see what has happened in past transactions (assuming the history_record config. option is set). You can use various command line options to view what happened, undo/redo/rollback to act on that information and start a new history file.

Prerequisite

You need yum version 3.2.xx or CentOS/RHEL 6.x. Verify this with the following command:

# yum info yum | grep --color Version

 

Sample outputs from CentOS v6.5:

Version : 3.2.29

 

yum history command syntax

The basic syntax is:

yum history

yum history command #ID

yum history [options]

history [info|list|packages-list|summary|addon-info|redo|undo|rollback|new]

 

Examples

First, verify that history has been configured for yum command.

# ls -l /var/lib/yum/history/

 

Sample outputs:

total 1036

drwxr-xr-x. 30 root root 4096 Dec 9 11:40 2012-11-08

-rw-------. 1 root root 1050624 Dec 9 11:40 history-2012-11-08.sqlite

 

List your history

Type the following command:

# yum history

OR

# yum history list

Sample outputs:

clip_image001[4]

Fig.01: yum history in action


Where,

ID - Transaction number.

Login user - The name of the user whose login session was used to initiate a yum.

Date and time - The date and time when a transaction was issued by yum.

Action(s) - A list of actions that were performed during a transaction as follows:

D or Downgrade - Package has been downgraded to an older version.

E or Erase - Package has been removed.

I or Install - New package has been installed.

O or Obsoleting - Package has been marked as obsolete.

R or Reinstall - Package has been reinstalled.

U or Update - Package has been updated to a newer version.

Altered — The number of packages that were affected by a yum, as follows:

*lt; - Before the transaction finished, the rpmdb database was changed outside yum.

> - After the transaction finished, the rpmdb database was changed outside yum.

* - The transaction failed to finish.

# - The transaction finished successfully, but yum returned a non-zero exit code.

E - The transaction finished successfully, but an error or a warning was displayed.

P - The transaction finished successfully, but problems already existed in the rpmdb database.

s - The transaction finished successfully, but the --skip-broken command line option was used and certain packages were skipped.

To see all transactions, pass the add option as follows:
# yum history list all

You can list only one transactions or transactions in a given range:

# yum history list 7

# yum history list 1..3

 

Sample outputs:

Loaded plugins: downloadonly, fastestmirror, security

ID | Login user | Date and time | Action(s) | Altered

-------------------------------------------------------------------------------

3 | 106300 | 2013-08-12 15:14 | Update | 1

2 | 88177 | 2012-11-09 14:24 | Install | 2

1 | System | 2012-11-08 19:19 | Install | 380

history list

 

List summary of all all past transactions

The syntax is:

# yum history summary

# yum history summary 3

# yum history summary 1..3


Sample outputs:

Loaded plugins: downloadonly, fastestmirror, security

Login user | Time | Action(s) | Altered

-------------------------------------------------------------------------------

<vivek> | Last day | Install | 1

<vivek> | Last week | E, I | 3

root <root> | Last week | Update | 1

<vivek> | Last 2 weeks | I, U | 139

<vivek> | Last 3 months | Update | 1

root <root> | Last 3 months | I, O, U | 284

106300 | Last 6 months | I, U | 6

88177 | Over a year ago | Install | 2

System <unset> | Over a year ago | Install | 380

history summary

 

Find out history of a package called nginx

The basic syntax is:

# yum history list PackageNameHere

# yum history list nginx

 

Sample outputs:

Loaded plugins: downloadonly, fastestmirror, security

ID | Login user | Date and time | Action(s) | Altered

-------------------------------------------------------------------------------

28 | <vivek> | 2013-12-09 11:40 | Install | 1 EE

history list

 

You can also see complete history as follows for mysql package including version number, run:

# yum history package-list mysql

 

Sample outputs:

Loaded plugins: downloadonly, fastestmirror, security

ID | Action(s) | Package

-------------------------------------------------------------------------------

26 | Install | mysql-5.1.71-1.el6.x86_64

25 | Erase | mysql-5.1.71-1.el6.x86_64 EE

24 | Install | mysql-5.1.71-1.el6.x86_64

history package-list

 

Now, use ID #24 to get detailed information on mysql package:

# yum history info 24

 

Sample outputs:

Loaded plugins: downloadonly, fastestmirror, security

Transaction ID : 24

Begin time : Mon Dec 2 11:35:59 2013

Begin rpmdb : 401:d28899f68a6631be573730605bd4825fe661fcce

End time : 11:36:03 2013 (4 seconds)

End rpmdb : 405:47873bf26c150bf0a26279da5c62d4ae4b1c227d

User : <vivek>

Return-Code : Success

Command Line : install mysql-server mysql

Transaction performed with:

Installed rpm-4.8.0-37.el6.x86_64 @base

Installed yum-3.2.29-40.el6.centos.noarch @base

Installed yum-plugin-fastestmirror-1.1.30-14.el6.noarch @base

Packages Altered:

Install mysql-5.1.71-1.el6.x86_64 @base

Install mysql-server-5.1.71-1.el6.x86_64 @base

Dep-Install perl-DBD-MySQL-4.013-3.el6.x86_64 @base

Dep-Install perl-DBI-1.609-4.el6.x86_64 @base

history info

 

To find out what additional information is available for a certain transaction:

# yum history addon-info id

# yum history addon-info 24

 

To just get info on last transaction:

# yum history addon-info last

 

How do I undo / revert transactions?

Say, you deleted the nginx package using yum command:

# yum erase ngnix

Now, use yum history command to review the transaction history, the yum history command provides means to revert or repeat a selected transaction. To revert (undo) a transaction, type the following at a shell prompt as root:

# yum history undo id

To undo nginx erase action, type:

# yum history

# yum history package-list nginx

 

Sample outputs:

Loaded plugins: downloadonly, fastestmirror, security

ID | Action(s) | Package

-------------------------------------------------------------------------------

29 | Erase | nginx-1.4.4-1.el6.ngx.x86_64

28 | Install | nginx-1.4.4-1.el6.ngx.x86_64 EE

history package-list

 

To undo use ID # 29:

# yum history undo 29

 

Verify that nginx has been installed again:

# yum history package-list nginx

Loaded plugins: downloadonly, fastestmirror, security

ID | Action(s) | Package

-------------------------------------------------------------------------------

30 | Install | nginx-1.4.4-1.el6.ngx.x86_64 EE

29 | Erase | nginx-1.4.4-1.el6.ngx.x86_64

28 | Install | nginx-1.4.4-1.el6.ngx.x86_64 EE

history package-list

How do I redo / repeat transactions?

To repeat a particular transaction, run:

# yum history redo id

 

The yum history rollback option

The syntax is:

# yum history rollback id

The rollback command will undo all transactions up to the point of the specified transaction. For example, if you have 3 transactions, where package A; B and C where installed respectively. Then "yum history undo 1" will try to remove package A, "yum history redo 1" will try to install package A (if it is not still installed), and "yum history rollback 1" will try to remove packages B and C.

The yum new option

As described above, the yum command stores the transaction history in a single SQLite database file in /var/lib/yum/history/ directory. To start new transaction history, run the following command:

# yum history new

 

Verify with the following command:

# yum history

I strongly suggest that you read yum command man page for more information

Linux / UNIX: Run Commands When You Log Out

Almost all modern shell including bash allows you run run command when you log out. Typically this is used to:

1.     Clean up screen with clear command.

2.     Remove history and other temporary file.

3.     Run commands or scripts and so on.

 

Logout file name

Commands in .logout are run when you log out.

1.     bash shell: ~/.bash_logout

2.     tcsh / csh: ~/.logout

 

Edit $HOME/.bash_logout and add your command:

$ vi ~/.bash_logout

 

Sample logout configuration:

if [ "$SHLVL" = 1 ]; then

#clear screen

[ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q

# delete mysql history

[ -f $HOME/.mysql_history ] && /bin/rm $HOME/.mysql_history

# Update ip accounting

[ -x /usr/bin/ip_accouting ] && /usr/bin/ip_accouting -u "$USER" -a

# call your script here

[ -x /usr/local/bin/timesheet_client.pl ] && /usr/local/bin/timesheet_client.pl

fi

 

A Note About Old Shell And Bourne / KSH Shell

Older UNIX shell and the Bourne / ksh shell don't have a logout file. So edit ~/.profile file, enter:

$ vi ~/.profile

 

Next append the following line:

trap '. $HOME/.my_shell_logout; exit' 0

 

Save and close the file. Finally create $HOME/.my_shell_logout, enter:

$ vi $HOME/.my_shell_logout

 

Sample config

# call your script here

if [ -f /usr/local/bin/timesheet_client.pl ]

then

/usr/local/bin/timesheet_client.pl

fi

Technorati Tags:

RHEL 7/OEL 7/CentOS 7: Configuring automatic startup of Oracle DB under systemd

There are several methods how to start Oracle Database automatically during/after OS boot. You can use Oracle CRS, other clusterware or init implemented in Linux. Starting RHEL 7 SysV init has been replaced by systemd or simply said systemd is the new init system.

In these days not only group of Linux users became polarized but also whole Linux world became polarized as well. Mostly Redhat based distributions have adopted systemd, other distributions are reluctant to implement systemd and either are continuing to use SysV init or migrated to another solutions e.g. Upstart. Despite this fact most of the Oracle certified Linux distributions (such as Redhat or SuSE and their (even not certified) clones) are using the systemd.

This post briefly shows how to configure systemd service for automatic start of Oracle Databases and Listener and these steps are applicable for Redhat Enterprise Linux 7, Oracle Enterprise Linux 7, CentOS 7 or SuSE Linux Enteprise Server 12 and Fedora 15 (or later).

 

Typically systemd startup configuration consists of two parts:

 

§  unit file - using ".service" suffix (in case of service), typically stored in /usr/lib/systemd/system or/etc/systemd/system directory for units provided by installed packages or /usr/lib/systemd/user or/etc/systemd/user directory for units installed by administrator

§  environment file (optional) - typically stored in /etc/sysconfig directory on RHEL and it's clones. We don't need it in our case.

 

Creating the unit for automatic startup/shutdown of Oracle Database manually

Logon as root user create and edit /etc/systemd/system/oracle-rdbms.service and add following content:

 

# /etc/systemd/system/oracle-rdbms.service

# Invoking Oracle scripts to start/shutdown Instances defined in /etc/oratab

# and starts Listener

[Unit]

Description=Oracle Database(s) and Listener

Requires=network.target

[Service]

Type=forking

Restart=no

ExecStart=/opt/oracle/12102/bin/dbstart /opt/oracle/12102

ExecStop=/opt/oracle/12102/bin/dbshut /opt/oracle/12102

User=oracle

[Install]

WantedBy=multi-user.target

 

Note that this configuration assumes that our ORACLE_HOME is /opt/oracle/12102. It's recommended to use PIDFile while using "forking" type but we don't need it. As you can see well known scripts (shipped with Oracle Database) are executed for startup/shutdown using path to Oracle Home in order to specify the Oracle Home for Listener process. As shown these scripts are executed under "oracle" user account/privileges. More over service can be started once network is cofigured (started) and service starts in multi-user level (more less equivalent of runlevel 3 in SysV init)

Now we have to reload systemd in order to register unit file (as root) and enable the service.

 

systemctl daemon-reload

systemctl enable oracle-rdbms

 

 So, now the startup service should be created and enabled but to be sure we can check it by following command (Note: first line is OS command, other lines is the output):

 

systemctl status oracle-rdbms

oracle-rdbms.service - Oracle Database(s) and Listener

Loaded: loaded (/etc/systemd/system/oracle-rdbms.service; enabled)

 

According to output our service is enabled succesfully and should be started on next OS boot. To start the service without reboot of machine you can use following command:

 

systemctl start oracle-rdbms

 

Creating the unit for automatic startup/shutdown of Oracle Database using script

I have created a simple script which automatically performs above tasks for creating and enabling startup service. This scripts contains simple checks (as I've tried to make the script bulletproof), then lists available Oracle homes that exist on OS and then asks to specify Oracle home from which the Listener will be started. Note that it's important to specify Oracle home for the highest version of Oracle software as Listener will be handling connections for all Oracle homes.

 

#!/usr/bin/bash

# This script configures systemd startup service for Oracle Databases and Listener

# Ivan Kartik http://ivan.kartik.sk

if [ `whoami` != "root" ]; then

echo "root login required!"

exit

fi

if [ `uname -s` != "Linux" ]; then

echo "This is not Linux!"

exit

fi

if [ `ps -e|grep " 1 ?"|cut -d " " -f15` != "systemd" ]; then

echo "Systemd is not present, use Init scripts instead!"

exit

fi

echo "List of existing Oracle Homes:"

echo "------------------------------"

cat `cat /etc/oraInst.loc|grep inventory_loc|cut -d '=' -f2`/ContentsXML/inventory.xml|grep "HOME NAME"|cut -d '"' -f 4

echo

echo "Enter ORACLE_HOME of Oracle Listener [$ORACLE_HOME]:"

read NEWHOME

case "$NEWHOME" in

"") ORAHOME="$ORACLE_HOME" ;;

*) ORAHOME="$NEWHOME" ;;

esac

if [ -z $ORAHOME ]; then

echo "Error: Missing value!"

exit

fi

if [ -f $ORAHOME/bin/lsnrctl ]; then

echo '# /etc/systemd/system/oracle-rdbms.service

# Ivan Kartik http://ivan.kartik.sk

# Invoking Oracle scripts to start/shutdown Instances defined in /etc/oratab

# and starts Listener

[Unit]

Description=Oracle Database(s) and Listener

Requires=network.target

[Service]

Type=forking

Restart=no

ExecStart='$ORAHOME'/bin/dbstart '$ORAHOME'

ExecStop='$ORAHOME'/bin/dbshut '$ORAHOME'

User=oracle

[Install]

WantedBy=multi-user.target' > /etc/systemd/system/oracle-rdbms.service

systemctl daemon-reload

systemctl enable oracle-rdbms

echo "Done! Service oracle-ordbms has been configured and will be started during next boot."

echo "If you want to start service now, execute: systemctl start oracle-rdbms"

else

echo "Error: No Listener script under specified ORACLE_HOME: $ORAHOME"

exit

fi

 

Final check of service and started databases (Note: Output from systemctl status has been shortened):

 

# cat /etc/oratab |grep :Y

ORA12CR1:/opt/oracle/12102:Y

ORA11GR2:/opt/oracle/11204:Y

# systemctl status oracle-rdbms

oracle-rdbms.service - Oracle Database(s) and Listener

Loaded: loaded (/etc/systemd/system/oracle-rdbms.service; enabled)

Active: active (running) since Mon 2015-11-15 14:51:13 CET; 54s ago

Process: 425 ExecStart=/opt/oracle/12102/bin/dbstart /opt/oracle/12102 (code=exited, status=0/SUCCESS)

CGroup: /system.slice/oracle-rdbms.service

+- 452 /opt/oracle/12102/bin/tnslsnr LISTENER -inherit

+-1155 ora_pmon_ORA12CR1

+-1171 ora_vktm_ORA12CR1

+-1177 ora_gen0_ORA12CR1

+-1181 ora_mman_ORA12CR1

+-1183 ora_diag_ORA12CR1

+-1185 ora_dbrm_ORA12CR1

+-1195 ora_ckpt_ORA12CR1

+-1197 ora_smon_ORA12CR1

+-1199 ora_reco_ORA12CR1

+-1201 ora_lreg_ORA12CR1

+-1289 ora_mman_ORA11GR2

.....

+-1291 ora_dbw0_ORA11GR2

+-1293 ora_lgwr_ORA11GR2

+-1295 ora_ckpt_ORA11GR2

+-1297 ora_smon_ORA11GR2

+-1299 ora_reco_ORA11GR2

+-1301 ora_mmon_ORA11GR2

+-1303 ora_mmnl_ORA11GR2

+-1350 ora_qmnc_ORA11GR2

+-1450 ora_q001_ORA11GR2

Nov 15 14:50:57 oel01 dbstart[425]: Processing Database instance "ORA12CR1": log file /opt/oracle/12102/startup.log

Nov 15 14:51:07 oel01 dbstart[425]: Processing Database instance "ORA11GR2": log file /opt/oracle/11204/startup.log

Nov 15 14:51:13 oel01 systemd[1]: Started Oracle Database(s) and Listener.

 

For little comparison of difference commands or usage regarding SysV init and systemd, here is very nice cheat sheet created by guys from Linoxide.com it's downloadable here: http://images.linoxide.com/systemd-vs-sysVinit-cheatsheet.pdf

Systemd Homepage: http://www.freedesktop.org/wiki/Software/systemd/