[20180608]asmcmd显示文件的日期时间问题.txt
http://blog.iarsov.com/oracle/date-and-time-format-in-asmcmd-for-file-listing/
--//如果你使用asmcmd查看asm文件,你可以发现日期时间的显示格式:
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
ASMCMD [+data/fyhis/PARAMETERFILE] > ls -l
Type Redund Striped Time Sys Name
PARAMETERFILE UNPROT COARSE JUN 08 00:00:00 Y spfile.272.931438543
--//仅仅显示日期,而时分秒都是0.
ASMCMD [+data/fyhis/ARCHIVELOG/2017_02_09] > ls -ls thread_1_seq_3773.3605.935537807
Type Redund Striped Time Sys Block_Size Blocks Bytes Space Name
ARCHIVELOG UNPROT COARSE FEB 09 2017 Y 512 937 479744 1048576 thread_1_seq_3773.3605.935537807
--//而实际上asmcmd是shell 脚本,最后运行的是perl脚本.
$ file $(which asmcmd)
/u01/app/11.2.0/grid/bin/asmcmd: POSIX shell script text executable
--//链接:http://blog.iarsov.com/oracle/date-and-time-format-in-asmcmd-for-file-listing/
ASMCMD utility base module is asmcmdbase.pm
The subroutine for listing files located in the base module is asmcmdbase_ls_process_file. You can see the logic how
file dates are printed. We can notice that if the file is older than 6 months the format used is "MON DD YYYY". If the
file is newer, the format used is "MON DD HH24:MI:SS". This subroutine is called (executed) for each file that needs to
be printed in ASMCMD.
List entries $entry_info_ref->{'mod_date'} and $entry_info_ref->{'mod_time'} contain the data for both hardcoded
formats. MOD_DATE if the files is older than 6 months and MOD_TIME if the file is newer.
sub asmcmdbase_ls_process_file
{
my ($dbh, $entry_info_ref, $args_ref, $cur_date, $file_info) = @_;
my ($related_alias); # The user or system alias for its corresponding #
# system or user alias, respectively. #
# Calculate dates only if we have file info from v$asm_file.
if ($file_info)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
# Use separate date format if date older than half a year.
if (($cur_date - $entry_info_ref->{'julian_date'}) >= $ASMCMDBASE_SIXMONTH)
{ # If older than six months, use 'MON DD YYYY' format. #
$entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_date'};
}
else
{ # Otherwise, use 'MON DD HH24:MI:SS' format. #
$entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time'};
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
--//说明:如果旧于6个月,显示日期.use 'MON DD YYYY' format.其他使用use 'MON DD HH24:MI:SS' format. #.
--//实际上这种模式与os的ls命令一样,我个人非常不习惯这种风格,我喜欢格式'YYYY/MM/DD HH24:MI:SS',这样国人的显示风格一直.
$ locate asmcmdbase.pm
/u01/app/11.2.0/grid/lib/asmcmdbase.pm
/u01/app/oracle/product/11.2.0/dbhome_1/lib/asmcmdbase.pm
--//检索sub asmcmdbase_ls_process_file,就可以定位以上信息.
--//按照链接的提示修改,注意修改前一定要做好备份.
# cp asmcmdbase.pm asmcmdbase.pm.ORG
# cp asmcmdshare.pm asmcmdshare.pm.ORG
--//修改asmcmdshare.pm文件:
# diff -Nur asmcmdshare.pm.ORG asmcmdshare.pm
--- asmcmdshare.pm.ORG 2018-06-08 09:00:56.566081223 +0800
+++ asmcmdshare.pm 2018-06-08 09:05:26.920081038 +0800
@@ -1916,11 +1916,14 @@
my ($sth, $qry, $row, $cmpd_ind);
$cmpd_ind = $gnum * (1 << 24) + $fnum;
+
+ if(!$ENV{'NLS_DATE_FORMAT_XX'}){ $ENV{'NLS_DATE_FORMAT_XX'} = "yyyy/mm/dd hh24:mi:ss"; }
$qry = 'select group_number, file_number, incarnation, block_size, ' .
'blocks, bytes, space, type, redundancy, striped, creation_date, ' .
'user_number, usergroup_number, permissions, ' .
'to_char(modification_date, \'MON DD HH24:MI:SS\') "MOD_TIME", ' .
+ 'to_char(modification_date, \''.$ENV{'NLS_DATE_FORMAT_XX'}.'\') "MOD_TIME_XX", ' .
'to_char(modification_date, \'MON DD YYYY\') "MOD_DATE", ' .
'to_char(modification_date, \'J HH24 MI SS\') "JULIAN_TIME", ' .
'to_char(modification_date, \'J\') "JULIAN_DATE" ' .
@@ -1945,6 +1948,7 @@
$file_info_ref->{'creation_date'} = $row->{'CREATION_DATE'};
$file_info_ref->{'mod_time'} = $row->{'MOD_TIME'};
$file_info_ref->{'mod_date'} = $row->{'MOD_DATE'};
+ $file_info_ref->{'mod_time_xx'} = $row->{'MOD_TIME_XX'};
$file_info_ref->{'julian_time'} = $row->{'JULIAN_TIME'};
$file_info_ref->{'julian_date'} = $row->{'JULIAN_DATE'};
$file_info_ref->{'user_number'} = $row->{'USER_NUMBER'};
--//修改asmcmdbase.pm文件:
# diff -Nur asmcmdbase.pm.ORG asmcmdbase.pm
--- asmcmdbase.pm.ORG 2018-06-08 08:59:26.442748126 +0800
+++ asmcmdbase.pm 2018-06-08 09:08:48.659080899 +0800
@@ -2044,6 +2044,8 @@
{ # Otherwise, use 'MON DD HH24:MI:SS' format. #
$entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time'};
}
+ # Overwrite date_print
+ $entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time_xx'};
}
# Find system alias only if we have info from v$asm_file, i.e., if we're
--//定义环境变量:
$ export NLS_DATE_FORMAT_XX="yyyy/mm/dd hh24:mi:ss"
ASMCMD [+Data/Fyhis/PARAMETERFILE] > ls -l
Type Redund Striped Time Sys Name
Use of uninitialized value $print_args[5] in printf at /u01/app/11.2.0/grid/lib/asmcmdbase.pm line 2369, <STDIN> line 7.
PARAMETERFILE UNPROT COARSE 2018/06/08 00:00:00 Y spfile.272.931438543
ASMCMD [+data/fyhis/ARCHIVELOG/2017_02_09] > ls -ls thread_1_seq_3773.3605.935537807
Type Redund Striped Time Sys Block_Size Blocks Bytes Space Name
Use of uninitialized value $print_args[5] in printf at /u01/app/11.2.0/grid/lib/asmcmdbase.pm line 2369, <STDIN> line 14.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ARCHIVELOG UNPROT COARSE 2017/02/09 23:00:00 Y 512 937 479744 1048576 thread_1_seq_3773.3605.935537807
--//有错.
--//我仔细看了一下脚本:
# vim asmcmdbase.pm
..
..
2306 if ($entry_ref->{'alias_directory'} eq 'Y')
2307 { # Directries have the suffix '/'. #
2308 $entry_ref->{'name_print'} .= '/';
2309 }
2310 elsif (defined ($args_ref->{'l'}))
2311 { # Date values exists for files only. #
2312 ($mon, $day, $time) = split (/\s+/, $entry_ref->{'date_print'});
~~~~~~~~//这里要拆分3个参数,必须修改如下,这样就存在3个参数:
+ if(!$ENV{'NLS_DATE_FORMAT_XX'}){ $ENV{'NLS_DATE_FORMAT_XX'} = "yyyy/mm/dd hh24:mi:ss "; }
~~~~~~~~~//注意我在ss后面加入1个空格,当然对应的不是月,日,时间.
2313 }
2314
2315
2316
2317 if (($entry_ref->{'alias_directory'} eq 'Y') ||
2318 (($entry_ref->{'system_created'} eq 'N') &&
2319 (!defined ($args_ref->{'L'}))))
2320 {
2321 $entry_ref->{'type'} = $ASMCMDBASE_SPACE;
2322 $entry_ref->{'redundancy'} = $ASMCMDBASE_SPACE;
2323 $entry_ref->{'striped'} = $ASMCMDBASE_SPACE;
2324 $mon = $ASMCMDBASE_SPACE;
2325 $day = $ASMCMDBASE_SPACE;
2326 $time = $ASMCMDBASE_SPACE;
2327 $entry_ref->{'space'} = $ASMCMDBASE_SPACE;
2328
2329 $entry_ref->{'block_size'} = $ASMCMDBASE_SPACE;
2330 $entry_ref->{'blocks'} = $ASMCMDBASE_SPACE;
2331 $entry_ref->{'bytes'} = $ASMCMDBASE_SPACE;
2332
2333 $entry_ref->{'user'} = $ASMCMDBASE_SPACE;
2334 $entry_ref->{'group'} = $ASMCMDBASE_SPACE;
2335 $entry_ref->{'perm'} = $ASMCMDBASE_SPACE;
2336 }
2337
2338 @print_args = ();
2339
2340 if (defined($args_ref->{'l'})) # ls -l. #
2341 {
2342 push (@print_args, $entry_ref->{'type'});
2343 push (@print_args, $entry_ref->{'redundancy'});
2344 push (@print_args, $entry_ref->{'striped'});
2345 push (@print_args, $mon);
2346 push (@print_args, $day);
2347 push (@print_args, $time);
2348 push (@print_args, $ASMCMDBASE_SPACE);
2349 push (@print_args, $entry_ref->{'system_created'});
2350 }
2351
2352 if (defined($args_ref->{'s'})) # ls -s. #
2353 {
2354 push (@print_args, $entry_ref->{'block_size'});
2355 push (@print_args, $entry_ref->{'blocks'});
2356 push (@print_args, $entry_ref->{'bytes'});
2357 push (@print_args, $entry_ref->{'space'});
2358 }
2359
2360 if (defined($args_ref->{'permission'})) # ls --permission -l. #
2361 {
2362 push (@print_args, $entry_ref->{'user'});
2363 push (@print_args, $entry_ref->{'group'});
2364 push (@print_args, $entry_ref->{'perm'});
2365 }
2366
2367 push(@print_args, $entry_ref->{'name_print'});
2368
2369 printf ($row, @print_args);
2370
2371 return;
2372 }
--//修改后在执行OK.不再报错.也许作者修改的版本与我的不同.
ASMCMD [+] > ls +DATA/FYHIS/PARAMETERFILE -ls
Type Redund Striped Time Sys Block_Size Blocks Bytes Space Name
PARAMETERFILE UNPROT COARSE 2018/06/08 00:00:00 Y 512 13 6656 1048576 spfile.272.931438543
ASMCMD [+] > ls +DATA/FYHIS/PARAMETERFILE -l
Type Redund Striped Time Sys Name
PARAMETERFILE UNPROT COARSE 2018/06/08 00:00:00 Y spfile.272.931438543
--//还有1个缺点就是sys,name列出现偏移.
--//当然也一些变通的方法,比如定义环境变量:
export NLS_DATE_FORMAT_XX="YY/MM/DD HH24 "
--//注意引号后有1个空格,这样就认为有3个字段.
ASMCMD [+] > ls +DATA/FYHIS/PARAMETERFILE -l
Type Redund Striped Time Sys Name
PARAMETERFILE UNPROT COARSE 18/06/08 00 Y spfile.272.931438543
--//但是最后2列还是存在偏移.
--//有点乱,最后修改如下:
# diff -Nur asmcmdshare.pm.ORG asmcmdshare.pm
--- asmcmdshare.pm.ORG 2018-06-08 09:00:56.566081223 +0800
+++ asmcmdshare.pm 2018-06-08 10:14:21.633059695 +0800
@@ -1916,11 +1916,14 @@
my ($sth, $qry, $row, $cmpd_ind);
$cmpd_ind = $gnum * (1 << 24) + $fnum;
+
+ if(!$ENV{'NLS_DATE_FORMAT_XX'}){ $ENV{'NLS_DATE_FORMAT_XX'} = "YYYY/MM/DD HH24:MI:SS "; }
$qry = 'select group_number, file_number, incarnation, block_size, ' .
'blocks, bytes, space, type, redundancy, striped, creation_date, ' .
'user_number, usergroup_number, permissions, ' .
'to_char(modification_date, \'MON DD HH24:MI:SS\') "MOD_TIME", ' .
+ 'to_char(modification_date, \''.$ENV{'NLS_DATE_FORMAT_XX'}.'\') "MOD_TIME_XX", ' .
'to_char(modification_date, \'MON DD YYYY\') "MOD_DATE", ' .
'to_char(modification_date, \'J HH24 MI SS\') "JULIAN_TIME", ' .
'to_char(modification_date, \'J\') "JULIAN_DATE" ' .
@@ -1945,6 +1948,7 @@
$file_info_ref->{'creation_date'} = $row->{'CREATION_DATE'};
$file_info_ref->{'mod_time'} = $row->{'MOD_TIME'};
$file_info_ref->{'mod_date'} = $row->{'MOD_DATE'};
+ $file_info_ref->{'mod_time_xx'} = $row->{'MOD_TIME_XX'};
$file_info_ref->{'julian_time'} = $row->{'JULIAN_TIME'};
$file_info_ref->{'julian_date'} = $row->{'JULIAN_DATE'};
$file_info_ref->{'user_number'} = $row->{'USER_NUMBER'};
# diff -Nur asmcmdbase.pm.ORG asmcmdbase.pm
--- asmcmdbase.pm.ORG 2018-06-08 08:59:26.442748126 +0800
+++ asmcmdbase.pm 2018-06-08 10:12:15.591059782 +0800
@@ -2044,6 +2044,8 @@
{ # Otherwise, use 'MON DD HH24:MI:SS' format. #
$entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time'};
}
+ # Overwrite date_print
+ $entry_info_ref->{'date_print'} = $entry_info_ref->{'mod_time_xx'};
}
# Find system alias only if we have info from v$asm_file, i.e., if we're