-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogtailer.py
82 lines (63 loc) · 2.58 KB
/
logtailer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /usr/bin/python
# Copyright (C) <2018> <Akshat Singh>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Script for tailing the logs specified in the configuration file."""
import os
import sys
from subprocess import call
import argparse
from argparse import RawTextHelpFormatter
# =========> Argument Parsing -- starts <========= #
# Command line argument validation functions...
def is_valid_file(parser, arg):
"Function for checking file exists or not."
if not os.path.isfile(arg):
parser.error('\n\n\tThe file {} does not exist!'.format(arg))
return arg
# Command line option
PARSER = argparse.ArgumentParser(
description='Log tailing script: '\
'For tailing logs specified in the config file.',
formatter_class=RawTextHelpFormatter)
PARSER.add_argument('-c', '--config_file',
help='Path of the configuration file.',
required=True, metavar='<Config File>',
type=lambda x: is_valid_file(PARSER, x))
ARGS = PARSER.parse_args()
# =========> Argument Parsing -- ends <========= #
INPUT_CONFIG_FILE = os.path.abspath(ARGS.config_file)
def build_string(sequence, seprator=' '):
"""Function for joining existing log files."""
return seprator.join(str(i) for i in sequence if os.path.isfile(i))
# Function for building the command
def cmnd_builder():
"""Function for building the part of the command"""
with open(INPUT_CONFIG_FILE, 'r') as config:
return build_string(config.read().splitlines())
config.close()
# Start execution of the main program
def main():
"""Main function to call the tail command"""
cmd = str(cmnd_builder()).strip()
if not cmd:
print("Warning: Either the config file is empty " \
"or the paths specified in the config file is/are incorrect!")
sys.exit()
else:
call(['tail -F ' + cmd], shell=True)
# Executing the script.
if __name__ == "__main__":
main()