Completed
Push — master ( f07019...0c998d )
by
unknown
18s queued 13s
created

monthly-report.gmp.main()   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 2
dl 13
loc 13
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2017-2019 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
from datetime import date, timedelta
20
21
from terminaltables import AsciiTable
22
23
24
def check_args(args):
25
    len_args = len(args.script) - 1
26
    if len_args < 2:
27
        message = """
28
        This script will display all vulnerabilities from the hosts of the
29
        reports in a given month!
30
        
31
        1. <month>  -- month of the monthly report
32
        2. <year>   -- year of the monthly report
33
34
        The third is 'with-tables' parameter to activate a verbose output of
35
        hosts. Explicitly made for GOS 3.1.
36
37
        Example:
38
            $ gvm-script --gmp-username name --gmp-password pass \
39
    ssh --hostname <gsm> scripts/monthly-report.gmp.py 05 2017 with-tables
40
        """
41
        print(message)
42
        quit()
43
44
45
def print_reports(gmp, args, from_date, to_date):
46
    report_filter = "rows=-1 and created>{0} and created<{1}".format(
47
        from_date.isoformat(), to_date.isoformat()
48
    )
49
50
    reports_xml = gmp.get_reports(filter=report_filter)
51
    report_list = reports_xml.xpath('report')
52
53
    sum_high = reports_xml.xpath(
54
        'sum(report/report/result_count/hole/full/' 'text())'
55
    )
56
    sum_medium = reports_xml.xpath(
57
        'sum(report/report/result_count/warning/' 'full/text())'
58
    )
59
    sum_low = reports_xml.xpath(
60
        'sum(report/report/result_count/info/full/' 'text())'
61
    )
62
63
    print('Found {0} reports'.format(len(report_list)))
64
65
    if 'with-tables' in args.script:
66
        for report in report_list:
67
            report_id = report.xpath('report/@id')[0]
68
            name = report.xpath('name/text()')[0]
69
70
            res = gmp.get_report(report_id)
71
72
            print('\nReport: {0}'.format(report_id))
73
74
            table_data = [
75
                ['Hostname', 'IP', 'Bericht', 'high', 'medium', 'low']
76
            ]
77
78
            for host in res.xpath('report/report/host'):
79
                hostname = host.xpath(
80
                    'detail/name[text()="hostname"]/../' 'value/text()'
81
                )
82
                if len(hostname) > 0:
83
                    hostname = str(hostname[0])
84
                else:
85
                    hostname = ""
86
87
                ip = host.xpath('ip/text()')[0]
88
                high = host.xpath('result_count/hole/page/text()')[0]
89
                medium = host.xpath('result_count/warning/page/text()')[0]
90
                low = host.xpath('result_count/info/page/text()')[0]
91
92
                table_data.append([hostname, ip, name, high, medium, low])
93
                host.clear()
94
                del host
95
96
            table = AsciiTable(table_data)
97
            print(table.table + '\n')
98
            res.clear()
99
            del res
100
101
    print(
102
        'Summary of results from {3} to {4}\nHigh: {0}\nMedium: {1}\nLow: '
103
        '{2}\n\n'.format(
104
            int(sum_high),
105
            int(sum_medium),
106
            int(sum_low),
107
            from_date.isoformat(),
108
            to_date.isoformat(),
109
        )
110
    )
111
112
113 View Code Duplication
def main(gmp, args):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
114
    # pylint: disable=undefined-variable
115
116
    check_args(args)
117
118
    month = int(args.script[1])
119
    year = int(args.script[2])
120
    from_date = date(year, month, 1)
121
    to_date = from_date + timedelta(days=31)
122
    # To have the first day in month
123
    to_date = to_date.replace(day=1)
124
125
    print_reports(gmp, args, from_date, to_date)
126
127
128
if __name__ == '__gmp__':
129
    main(gmp, args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
130