forked from redhat-performance/tuned
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuned-adm.py
executable file
·109 lines (90 loc) · 4.37 KB
/
tuned-adm.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python -Es
#
# tuned: daemon for monitoring and adaptive tuning of system devices
#
# Copyright (C) 2008-2013 Red Hat, Inc.
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
from __future__ import print_function
import argparse
import sys
import traceback
import tuned.admin
import tuned.consts as consts
import tuned.version as ver
from tuned.utils.global_config import GlobalConfig
def check_positive(value):
try:
val = int(value)
except ValueError:
val = -1
if val <= 0:
raise argparse.ArgumentTypeError("%s has to be >= 0" % value)
return val
if __name__ == "__main__":
config = GlobalConfig()
parser = argparse.ArgumentParser(description="Manage tuned daemon.")
parser.add_argument('--version', "-v", action = "version", version = "%%(prog)s %s.%s.%s" % (ver.TUNED_VERSION_MAJOR, ver.TUNED_VERSION_MINOR, ver.TUNED_VERSION_PATCH))
parser.add_argument("--debug", "-d", action="store_true", help="show debug messages")
parser.add_argument("--async", "-a", action="store_true", help="with dbus do not wait on commands completion and return immediately")
parser.add_argument("--timeout", "-t", default = consts.ADMIN_TIMEOUT, type = check_positive, help="with sync operation use specific timeout instead of the default %d second(s)" % consts.ADMIN_TIMEOUT)
subparsers = parser.add_subparsers()
parser_list = subparsers.add_parser("list", help="list available profiles")
parser_list.set_defaults(action="list")
parser_active = subparsers.add_parser("active", help="show active profile")
parser_active.set_defaults(action="active")
parser_off = subparsers.add_parser("off", help="switch off all tunings")
parser_off.set_defaults(action="off")
parser_profile = subparsers.add_parser("profile", help="switch to a given profile, or list available profiles if no profile is given")
parser_profile.set_defaults(action="profile")
parser_profile.add_argument("profiles", metavar="profile", type=str, nargs="*", help="profile name")
parser_profile_info = subparsers.add_parser("profile_info", help="show information/description of given profile or current profile if no profile is specified")
parser_profile_info.set_defaults(action="profile_info")
parser_profile_info.add_argument("profile", metavar="profile", type=str, nargs="?", default="", help="profile name, current profile if not specified")
if config.get(consts.CFG_RECOMMEND_COMMAND, consts.CFG_DEF_RECOMMEND_COMMAND):
parser_off = subparsers.add_parser("recommend", help="recommend profile")
parser_off.set_defaults(action="recommend_profile")
parser_verify = subparsers.add_parser("verify", help="verify profile")
parser_verify.set_defaults(action="verify_profile")
parser_verify.add_argument("--ignore-missing", "-i", action="store_true", help="do not treat missing/non-supported tunings as errors")
parser_auto_profile = subparsers.add_parser("auto_profile", help="enable automatic profile selection mode, switch to the recommended profile")
parser_auto_profile.set_defaults(action="auto_profile")
parser_profile_mode = subparsers.add_parser("profile_mode", help="show current profile selection mode")
parser_profile_mode.set_defaults(action="profile_mode")
args = parser.parse_args(sys.argv[1:])
options = vars(args)
debug = options.pop("debug")
async = options.pop("async")
timeout = options.pop("timeout")
action_name = options.pop("action")
result = False
dbus = config.get_bool(consts.CFG_DAEMON, consts.CFG_DEF_DAEMON)
try:
admin = tuned.admin.Admin(dbus, debug, async, timeout)
result = admin.action(action_name, **options)
except tuned.admin.TunedAdminException as e:
if not debug:
print(e, file=sys.stderr)
else:
traceback.print_exc()
sys.exit(2)
except:
traceback.print_exc()
sys.exit(3)
if result == False:
sys.exit(1)
else:
sys.exit(0)