summaryrefslogtreecommitdiffstats
path: root/pykolab/cli/telemetry/cmd_examine_command_issue.py
blob: 9563dbcd9bf357ef3711b6176d7b3e3e4befe130 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123

# -*- coding: utf-8 -*-
# Copyright 2010-2013 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# 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/>.
#

import pykolab

from pykolab.translate import _

log = pykolab.getLogger('pykolab.cli')
conf = pykolab.getConf()

from pykolab import telemetry
from pykolab.cli import commands

def __init__():
    commands.register('examine_command_issue', execute, group='telemetry', description="Examine a particular telemetry command issue.")

def execute(*args, **kw):
    db = telemetry.init_db()

    try:
        wanted = conf.cli_args.pop(0)
    except:
        log.error(_("Unspecified command issue identifier"))
        sys.exit(1)

    command_issue = db.query(
            telemetry.TelemetryCommandIssue
        ).filter_by(
                id=wanted
            ).first()

    if command_issue == None:
        log.error(_("Invalid command issue identifier"))
        sys.exit(1)

    session = db.query(
            telemetry.TelemetrySession
        ).filter_by(
                id=command_issue.session_id
            ).first()

    if session == None:
        log.error(_("Invalid session identifier"))
        sys.exit(1)

    user = db.query(
            telemetry.TelemetryUser
        ).filter_by(
                id=session.user_id
            ).first()

    server = db.query(
            telemetry.TelemetryServer
        ).filter_by(
                id=session.server_id
            ).first()

    print _("Session by %s on server %s") % (user.sasl_username,server.fqdn)

    command_issues = db.query(
            telemetry.TelemetryCommandIssue
        ).filter_by(
                session_id=session.id
            )

    for _command_issue in command_issues:
        command = db.query(
                telemetry.TelemetryCommand
            ).filter_by(
                    id=_command_issue.command_id
                ).first()

        command_arg = db.query(
                telemetry.TelemetryCommandArg
            ).filter_by(
                    id=_command_issue.command_arg_id
                ).first()

        if command_issue.id == _command_issue.id:
            print "========="

        print "Client(%d): %s %s %s" % (
                _command_issue.id,
                _command_issue.command_tag,
                command.command,
                command_arg.command_arg
            )

        server_responses = db.query(
                telemetry.TelemetryServerResponse
            ).filter_by(
                    command_issue_id=_command_issue.id
                )

        for server_response in server_responses:
            server_response_lines = server_response.response.split('\n');

            for server_response_line in server_response_lines:
                print "Server(%d): %s" % (
                        server_response.id,
                        server_response_line
                    )

        if command_issue.id == _command_issue.id:
            print "========="