Passed
Pull Request — master (#370)
by Jaspar
01:49
created

monthly-report.gmp.get_report_list()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2017-2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import sys
20
from datetime import date, timedelta
21
22
from terminaltables import AsciiTable
23
24
25
def check_args(args):
26
    len_args = len(args.script) - 1
27
    if len_args < 2:
28
        message = """
29
        This script will display all vulnerabilities from the hosts of the
30
        reports in a given month!
31
        
32
        1. <month>  -- month of the monthly report
33
        2. <year>   -- year of the monthly report
34
35
        The third is 'with-tables' parameter to activate a verbose output of
36
        hosts. Explicitly made for GOS 3.1.
37
38
        Example:
39
            $ gvm-script --gmp-username name --gmp-password pass \
40
    ssh --hostname <gsm> scripts/monthly-report.gmp.py 05 2017 with-tables
41
        """
42
        print(message)
43
        sys.exit()
44
45
46
def get_reports_xml(gmp, args, from_date, to_date):
47
    report_filter = "rows=-1 and created>{0} and created<{1}".format(
48
        from_date.isoformat(), to_date.isoformat()
49
    )
50
51
    return gmp.get_reports(filter=report_filter)
52
53
54
def print_result_sums(reports_xml, from_date, to_date):
55
    sum_high = reports_xml.xpath(
56
        'sum(report/report/result_count/hole/full/text())'
57
    )
58
    sum_medium = reports_xml.xpath(
59
        'sum(report/report/result_count/warning/full/text())'
60
    )
61
    sum_low = reports_xml.xpath(
62
        'sum(report/report/result_count/info/full/text())'
63
    )
64
65
    print(
66
        'Summary of results from {3} to {4}\nHigh: {0}\nMedium: {1}\nLow: '
67
        '{2}\n\n'.format(
68
            int(sum_high),
69
            int(sum_medium),
70
            int(sum_low),
71
            from_date.isoformat(),
72
            to_date.isoformat(),
73
        )
74
    )
75
76
77
def print_result_tables(gmp, reports_xml):
78
79
    report_list = reports_xml.xpath('report')
80
81
    for report in report_list:
82
        report_id = report.xpath('report/@id')[0]
83
        name = report.xpath('name/text()')[0]
84
85
        res = gmp.get_report(report_id)
86
87
        print('\nReport: {0}'.format(report_id))
88
89
        table_data = [['Hostname', 'IP', 'Bericht', 'high', 'medium', 'low']]
90
91
        for host in res.xpath('report/report/host'):
92
            hostname = host.xpath(
93
                'detail/name[text()="hostname"]/../' 'value/text()'
94
            )
95
            if len(hostname) > 0:
96
                hostname = str(hostname[0])
97
            else:
98
                hostname = ""
99
100
            ip = host.xpath('ip/text()')[0]
101
            high = host.xpath('result_count/hole/page/text()')[0]
102
            medium = host.xpath('result_count/warning/page/text()')[0]
103
            low = host.xpath('result_count/info/page/text()')[0]
104
105
            table_data.append([hostname, ip, name, high, medium, low])
106
            host.clear()
107
            del host
108
109
        table = AsciiTable(table_data)
110
        print(table.table + '\n')
111
        res.clear()
112
113
114
def get_report_list(gmp, args, from_date, to_date):
115
    report_list = reports_xml.findall('report')
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable reports_xml does not seem to be defined.
Loading history...
116
117
    print('Found {0} reports'.format(len(report_list)))
118
    return report_list
119
120
121
def main(gmp, args):
122
    # pylint: disable=undefined-variable
123
124
    check_args(args)
125
126
    month = int(args.script[1])
127
    year = int(args.script[2])
128
    from_date = date(year, month, 1)
129
    to_date = from_date + timedelta(days=31)
130
    # To have the first day in month
131
    to_date = to_date.replace(day=1)
132
133
    reports_xml = get_reports_xml(gmp, args, from_date, to_date)
134
135
    print_result_sums(reports_xml, from_date, to_date)
136
    if "with-tables" in args.script:
137
        print_result_tables(gmp, reports_xml)
138
139
140
if __name__ == '__gmp__':
141
    main(gmp, args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
142