LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2008, 08:59 AM   #1
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Rep: Reputation: 0
Lightbulb Script to send mail to admin after server idle for 5 days


Hi

I want to write a script which will check server idle time. And if its idle for more than 5 days (nobody logged in) then it will send a mail to admin saying that "this server ($HOSTNAME) is idle for 5 days"

How can I accomplish this?

Any help would be really appreciated...

Sid
 
Old 07-18-2008, 09:12 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

A machine isn't necessary idle if nobody has logged in for X days. I'm not talking about OS related processes but, for example, cronjobs that belong to specific users.

That said, you can use the last command to check who is/was logged in. There is one possible pitfall: last uses /var/log/wtmp to get this data and on a machine that has logrotate (or simular) running it can be that the information is (partially) in the rotated file.

Hope this gets you going.
 
Old 07-18-2008, 09:18 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
I would try lastlog -t 5. If the ouput is null, send a mail notification otherwise do nothing.
 
Old 07-18-2008, 09:57 AM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
I am just about to release a utility into the wild with the GPL which does exactly what you need. I have been using a more primitive variant of it here for awhile and have been cleaning it up and generalizing the code because others might like it too.

What it does is run as a cron job, and look for a specific file in /tmp. If it doesn't find it, it creates it. If it does find it, it checks the date stamp for the last modification time, then if enough time has elapsed (if the file is old enough) it sends emails to a contact list.

So, what you do is set up your login scripts so that upon login by any user this file is deleted. Thus, if the file is not deleted for five days, no one has logged in; send the email.

I should have it ready to release in the next few days. It is written in C.
 
Old 07-18-2008, 11:05 AM   #5
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Well....Thanks to all of you guys for ample replies.

Here I Go...

Reply to DRUUNA: the user who will login is ONLY root. So i can run a script as root user.

Reply to COLUCIX: I liked the command. but please tell me how can i write this command OUTPUT to a file? Once I can write the output I can send it to my mail.

I just wanted to know the SCRIPT to WRITE OUTPUT....plz help.

Reply JIML8: I am badly waiting for your script....

Thanks to All again
~S
 
Old 07-18-2008, 12:02 PM   #6
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
If you wanna do this right, the first place to look would probably be pam_lastlog.c
 
Old 07-18-2008, 12:41 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,
Quote:
Originally Posted by siddhartha_ece2004 View Post
Reply to DRUUNA: the user who will login is ONLY root. So i can run a script as root user.
I don't think you got the point. My reply hasn't got anything to do with the sort of user that is running a script, I pointed out that a user (root included) does not have to be logged on to run something. I hope you see the difference between a box that is idle and one where nobody is/was logged in (even if nobody is logged in the box can be running at full capacity).

Hope this clears things up
 
Old 07-19-2008, 08:51 AM   #8
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Well I got your point Druuna..... thats why I am mot going to do anything with the box. I just wanted to get details whether anybody logged into this box or, its just idle (user login point of view) for 5 days. I know its not necessary that somebody log-in into it and do something, a script in cronjob can perform anything in background. But, just to verify i wanted the details.

Anyway, i think lastlog -t 5 can perform this job.But If the ouput is null, send a mail notification otherwise do nothing how I will do this part in script???

Please let me know.......

~S
 
Old 07-19-2008, 09:45 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
islog=$(lastlog -t 5)
if [ -n $islog ]
then
   <command here>
   <command here>
   <...>
else
   <command here>
   <...>
fi
here you assign the output of lastlog to the islog variable, using command substitution. Then check if the length of islog is non-zero and act accordingly. If you prefer you can check if the length of islog is zero, using the test -z. See man test for future reference.
 
Old 07-21-2008, 01:04 AM   #10
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
No Go ...it throws this error:

line 2: [: too many arguments

~S
 
Old 07-21-2008, 03:49 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Sorry. The error is triggered by the blank spaces and newline characters inside the lastlog output. You simply have to embed $islog with double quotes to prevent this behavior:
Code:
islog=$(lastlog -t 5)
if [ -n "$islog" ]
then
   <command here>
   <command here>
   <...>
else
   <command here>
   <...>
fi
You can also put the command substitution in the test expression, without assigning a shell variable:
Code:
if [ -n "$(lastlog -t 5)" ]
then
   <command here>
   <command here>
   <...>
else
   <command here>
   <...>
fi
 
Old 07-22-2008, 02:11 AM   #12
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks! It Worked!!!

Just one more quick help...

How can I push this script (file) to all machines which are in LAN? I meant, I want to copy this script in /root/ for all the machines.

~S
 
Old 07-22-2008, 06:13 AM   #13
siddhartha_ece2004
LQ Newbie
 
Registered: Jul 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Well I got some clue. I heard about expect which will do remote copy to servers. Can anybody tell me how can it be done? I meant the script...

S
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command line email client to send mail to remote mail server dhanju Linux - Software 1 03-07-2008 07:37 AM
Send mail through script aabfm Linux - Networking 5 07-01-2006 05:46 AM
urgent: mail server down 2 days I_AM Linux - General 2 07-29-2005 10:59 AM
Server does'nt send mail until shutdown and LAN can't send or receive. Wolfy Linux - Networking 0 08-02-2004 08:31 PM
CGI Script runs to send mail, but mail is never sent robertwo Linux - Newbie 2 06-10-2004 10:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration