LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 01-28-2011, 02:16 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Rep: Reputation: 32
Red face shell/perl Script for file record


Hi,
I want to write a cron job for a script which should list out
1. new files added, owner of this file, date.
2. if some file deleted, by whom and date.

How can I write this script ?
Any idea would help me a lot.

Thanks,
Barun Parichha
 
Old 01-28-2011, 02:38 PM   #2
gb2312
LQ Newbie
 
Registered: Dec 2005
Posts: 20

Rep: Reputation: 6
I don't know if it's possible to figure out who deleted what file just from shell script, but maybe this program "iwatch" can help (I've never used it):

http://iwatch.sourceforge.net/index.html
 
Old 01-31-2011, 11:42 PM   #3
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
I can think of two possible approach at this moment:

1. Based on Inode table changes:
Is there any command available, which will trigger some script for inode-table changes ?

2.Based on storage of files in an array:
Info for Files in the folder will be stored in an array. And a cron job would check, if files in array r avaiable in folder. New files would be added to trail of array.
 
Old 02-05-2011, 02:25 PM   #4
gb2312
LQ Newbie
 
Registered: Dec 2005
Posts: 20

Rep: Reputation: 6
Looking at iwatch documentation, it can probably start a script whenever files changed:

http://iwatch.sourceforge.net/documentation.html

I don't know if you can get who deleted a file though.

Renaming is a major problem for method two: "mv a b" would be interpreted as "rm a" and "create b".

If you can accept the shortcoming of not known who changed/deleted files, I think using a combination of method 1 + 2 might work like this:

* assume the script is run be cron periodically to check what changed below a certain directory

* use command find to build a listing of files under that directory containing: i-node number, modification timestamp, pathname

For example (see man find, just an example)
Quote:
$ find /tmp/test -printf '%i %T@ %p\n'
1586145 1296936601.0000000000 /tmp/test
1586146 1296936591.0000000000 /tmp/test/a.txt
1586147 1296936594.0000000000 /tmp/test/b.txt
1586148 1296936604.0000000000 /tmp/test/c
1586149 1296936604.0000000000 /tmp/test/c/c.txt
* Now you can sort this list by i-node number and save as a snapshot of the directory

* Every time the script runs, it generate a new snapshot and compares it with the old one:

@ missing old inode: old file/directory deleted
@ same inode: compare timestamp and pathname for change
@ new inode: new file/directory created

* You might want to investigate more timestamps (man 2 stat: st_ctime), adding file type (file or directory) and other attributes for report (man find)
 
Old 02-10-2011, 06:19 AM   #5
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
Cron job:

Code:
*/2 * * * * /usr/bin/sh Myscript.sh
Myscript.sh :

Code:
#Script to run once in every 2 mins
#RecFile which store all transaction info
Touch RecFile
#PrevList contains old filenames 
touch PrevList
#PresList contains present filenames
ls -l| tr -s ' '| cut -d ' ' -f9 >PresList
#Writing files added to Record
echo "Files Added:\c" >>RecFile
for i in `comm -1 PrevList PresList`
do
echo "$i \c" >>RecFile
done
#Writing files removed to record
echo ":Files Removed:\c" >>RecFile
for i in `comm -1 PresList PrevList`
do
echo "$i \c">>RecFile
done
echo ":`date`\n">>RecFile
#Store present filelists to PrevList
mv PresList PrevList
 
Old 02-10-2011, 07:33 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by barunparichha View Post
I want to write a cron job for a script which should list out 1. new files added, owner of this file, date. 2. if some file deleted, by whom and date.
Is there a specific reason for doing or needing that? I'm asking because logging this type of auditing information doesn't need re-invention of the wheel. It is already provided for if you run the auditd service on a system with an audit-enabled kernel: just configure system call logging. Then with the accompanying reporting tools you can query the log for any creation / deletions.
 
Old 02-11-2011, 12:21 AM   #7
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
I agree with unSpawn's comment.
But I don't see any alternate way except writing a new script.
I am using Solaris 10 machine and I don't know if this is audit-enabled.

Is there a mechanism to find out, the command or user who deleted the file ?
 
Old 02-11-2011, 12:22 AM   #8
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
I agree with unSpawn's comment.
But I don't see any alternate way except writing a new script.
I am using Solaris 10 machine and I don't know if this is audit-enabled.

Is there a mechanism to find out, the command or user who deleted the file ?
 
Old 02-11-2011, 05:26 PM   #9
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Solaris and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-13-2011, 02:43 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
There are several ways to do it under Solaris. One would be to use a simple dtrace script which would report in real time every plain file creation and deletion. That one will do the job:

Code:
#!/usr/sbin/dtrace -qws
BEGIN
{
        printf("%6s\t%6s\t%12s\t%6s\t%s\n", "PID","UID","CMD","ACTION","FILE");
}
fop_create:entry
{
        self->create=args[5];
}
fop_create:return
/self->create/
{
        printf("%6d\t%6d\t%12s\tcreate\t%s\n", pid, uid, execname, stringof(((*self->create)->v_path)));
        self->create=0;
}

fop_remove:entry
{
        printf("%6d\t%6d\t%12s\tremove\t%s/%s\n", pid, uid, execname, stringof(args[0]->v_path), stringof(args[1]));
}
If you want something more configurable and integrated, you can also explore BSM (auditing framework) and Solaris extended accounting capabilities.

http://download.oracle.com/docs/cd/E...7q8/index.html
http://download.oracle.com/docs/cd/E...t-6/index.html
 
Old 02-14-2011, 03:07 AM   #11
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303

Original Poster
Rep: Reputation: 32
dtrace can be used only in super user mode.
What can be done as a normal user?
 
Old 02-14-2011, 11:21 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Knowing what files are created and deleted and by whom is a sensitive information. The user requiring this information need to be granted the required privilege. It need not necessarily be root though.
 
  


Reply



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
Update a record by matching a varaible with Mask in Shell Script suresh.chola Programming 2 01-19-2010 01:42 AM
Daemon shell or perl script to monitor a log file khriz Programming 4 01-07-2010 07:35 AM
Shell Script or perl help. to write sections of a log to a tmp file for mailing pobman Programming 2 02-02-2009 03:30 PM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 09:56 AM
Shell script to find/replace build new TAB record ljungers Programming 6 01-19-2007 04:47 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 03:34 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
Open Source Consulting | Domain Registration