#!/bin/bash
#return error code
#0: Success
#1: Parameter Error
#6: Custom.log file is empty
#7: Cut File Error
#8: Up log file success
Tar_dir_file_max=30
main_log_file_max=1024
Main_log_name= ' Custom.log '
Cut_main_log_file= "custom_$ (date +"%y%m%d%h%m "). LOG"
function usage ()
{
echo "Options:"
echo "-F:FLG"
echo "-t:custom.log tar path"
echo "-l:custom.log path"
echo "-h:show Help Info"
}
function Check_parameter ()
{
Test "X${log_flg}-${tar_path}-${main_log_path}" = "x--" && echo "Error:argment is empty" && usage &&A mp Exit 1
Test "X$LOG_FLG"! = "x1" && echo "error:-F FLG argment Error" && Usage && exit 1
Test "X$tar_path" = "x" && echo "Error:-T custom.log TAR PATH" && usage && exit 1
Test "X$main_log_path" = "x" && echo "Error:-L custom.log PATH" && usage && exit 1
[-F ' $MAIN _log_path] && echo "Error:-L must is dir" && usage && exit 1
[-F ' $TAR _path] && echo "Error:-T must is dir" && usage && exit 1
}
function Touch_main_file ()
{
Touch "${main_log_path}/${main_log_name}"
chmod 777 "${main_log_path}/${main_log_name}"
}
function Rm_tar_file ()
{
sum_tar_file= ' ls ${tar_path}/|wc-l '
If ["$SUM _tar_file"-gt "$TAR _dir_file_max"]; Then
' Find ${tar_path}/-mtime +2|xargs rm-f '
Fi
}
While Getopts:f:t:l: OPTION
Do
Case $OPTION in
F
log_flg= $OPTARG
;;
T
Tar_path= $OPTARG
;;
L) main_log_path= $OPTARG
;;
H
Usage
Exit 0
;;
\?)
echo "-$OPTARG: Invalid option"
Usage
Exit 1
;;
:)
echo "-$OPTARG: Miss option Argment"
Usage
Exit 1
;;
Esac
Done
# # # Check Parameters
Check_parameter
# # # Cutting
[!-F "${main_log_path}/${main_log_name}"] && Touch_main_file
[!-S "${main_log_path}/${main_log_name}"] && echo "${main_log_file}/${main_log_name} FILE is empty" && Exit 6
MV ${main_log_path}/${main_log_name} ${main_log_path}/${cut_main_log_file}
Test "x$"!! = "x0" && echo "cut ${main_log_path}/${main_log_name} File Error" && Exit 7
Touch_main_file
# # # Pack
Tar cjfp ${tar_path}/${cut_main_log_file}.tar.bz2 ${main_log_path}/${cut_main_log_file}
# # # Report
File_size= ' du ${main_log_path}/${cut_main_log_file}|awk ' {print $} '
["$FILE _size"-gt "$MAIN _log_file_max"] && uniq_file= ' Cat ${main_log_path}/${cut_main_log_file}|sort|uniq| Head-50 ' | | Uniq_file= ' Cat ${main_log_path}/${cut_main_log_file}|sort|uniq '
# # # Delete
Rm-f ${main_log_path}/${cut_main_log_file}
# # # # # Delete the oldest file in the Tar directory
Rm_tar_file
Echo-e "${file_size} (k) \n${uniq_file}" && exit 8
======= PHP
<?php
Class Sslog {
Public Function init () {
while (true) {
Sleep (60);
$output = Array ();
$cmd = "/script/sslog/sslog.sh-l/home/www/logs-t/home/www/logs/logtar-f 1 </dev/null 2>&1";
EXEC ($cmd, $output, $code);
if ($code ==6) {
Continue
}
$this->callbacklog (Array (
' Service_keyword ' = ' boss_system_alarm ',
' Rule_keyword ' = ' boss_system_alarm ',
' Title ' = ' Boss system error ',
' Content ' = implode (', ', $output)
));
}
}
Private Function Callbacklog ($logData) {
$postKey = MD5 (date (' YMD '));
$POSTURL = "Http://api.com/tigger_subscribe";
$postData = Json_encode (Array_merge (Array (
' Fctoken ' = $postKey,
' Fcname ' = ' syslog_monitor ',
), $logData));
$res = $this->postcurldata ($POSTURL, $postData);
if (Is_null (Json_decode ($res))) {
echo ' Callback URL error .... ';
}else{
$decodeRes = Json_decode ($res, true);
if (Isset ($decodeRes [' status ']) &&!empty ($decodeRes [' status ']) {
echo "Callback url--{$res}";
}
}
return $res;
}
Private Function Postcurldata ($get _url, $postdata = ", $other _options = Array ()) {
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $get _url);
curl_setopt ($curl, Curlopt_postfields, $postdata);
curl_setopt ($curl, Curlopt_post, true);
curl_setopt ($curl, Curlopt_timeout, 30);
if (!empty ($other _options[' userpwd ')) {
curl_setopt ($curl, curlopt_userpwd, $other _options[' userpwd ');
}
curl_setopt ($curl, Curlopt_header, 0);
curl_setopt ($curl, Curlopt_returntransfer, 1);
$ret = curl_exec ($curl);
if (Curl_errno ($curl)) {
return false;
}
Curl_close ($curl);
return $ret;
}
}
$sslog = new Sslog ();
$sslog->init ();
This article from the "Development and operation of the" blog, declined to reprint!
Shell Cutting Scripts