The following script will show you an example on how to monitor basic file permissions, for example wrong file mode, wrong file owner and mount point check.
Input/Output error is monitored by file_io_error which means there are some bad blocks on the partition, you may need to change a disk, or do fsck at mid-night.
Code:sub debug { my $return = shift; my $err_msg = shift || undef; if (defined $debug) { print "$err_msg\n" if (defined $err_msg); } if ($return > 100) { print $return; exit 0; } } sub file_owner($$) { my $file = shift; my $owner = shift; my $file_uid = ( stat $file )[4]; my $real_owner = ( getpwuid $file_uid )[0]; debug(322, "$file has a wrong owner") if ( $real_owner ne $owner ); } sub file_io_error($) { my $filename = shift; debug(1, "Check file IO error for $filename"); my @logs = `tail -n 1500 $filename`; foreach my $line (@logs) { debug(111, "$filename IO error. Possible bad blocks on disk!") if($line =~ /Input.Output error/); } } sub mount_point{ my $m_point = shift; debug(1, "Check mount point for $m_point"); my $proc_mounts = "/proc/mounts"; my (@mounts, @lines, $ok); open(FILE, $proc_mounts); while(<FILE>) { chomp(); push(@lines, $_); } close(FILE); foreach my $line (@lines) { my @tmp = split(/\s/,$line); push(@mounts,$tmp[1]); } foreach my $mount (@mounts) { my $tmp = $mount; if ($tmp =~ /^$m_point$/) { $ok = 1; } } debug(102, "$m_point is not mount point!") if ($ok != 1 ); } sub file_mode($$){ my $file = shift; debug(1, "Check file mode for $file"); my $umod = shift || undef; my $gmod = shift || undef; my $omod = shift || undef; use Fcntl ':mode'; my $mode = (stat($file))[2]; my $user_rwx = ($mode & S_IRWXU) >> 6; my $group_rwx= ($mode & S_IRWXG) >> 3; my $other_rwx = $mode & S_IRWXO; debug(223, "wrong owner permission: $file") if ( $user_rwx != $umod ); } sub data_fs_check_sample(){ foreach my $num (`seq -f %02g 0 100`) { chomp $num; my $data_path = "/data/$num"; if (-d $data_path) { mount_point($data_path); file_owner($data_path, "coremail"); file_mode($data_path, 7); } } }


LinkBack URL
About LinkBacks




Reply With Quote