summarylogtreecommitdiffstats
path: root/create_top_directions
blob: ce4811ea77aeeccd9dcb09261f6f5f52ceaed782 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/bash

#
# NOTE:
#
# The script is taken from the link on the website http://vasexperts.ru
# Edited by 4le34n
#

#
# Load configuration
.   /etc/nfsen/scripts.conf

#
# Subroutines
#   $1 - line
perform_action ()
{
    if [ $write_script = "y" ]
    then
        if [ "x$script_file" = "x" ]
        then
            echo $1;
        else
            echo $1 >> $script_file;
        fi;

    else
        eval $1;
    fi;
}

#
# Set default options
self=$0
show_usage="n"
consumers=10;
divide_up_down="n"
exclude_others_ports="n"
begin_coll_date=`date +%F-00-00`;
end_coll_date="";
profile_name=""
shadow=1;
ports_desc="/etc/nfsen/asnum.desc"
write_script="n"
script_file=""

#
# Process command line 
while [ "$#" != "0" ]
do
    case "$1" in
        "-h" | "--help")
            show_usage="y";
            break;
        ;;

        "-c" | "--consumers")
            shift;

            if [ "$#" = "0" ]
            then
                echo "The number of consumers is missed."
                show_usage="y";
                break;
            fi;

            consumers=$1;
        ;;

        "-b" | "--begin-collecting")
            shift;

            if [ "$#" = "0" ]
            then
                echo "The date is missed."
                show_usage="y";
                break;
            fi;

            begin_coll_date=$1;
        ;;

        "-e" | "--end-collecting")
            shift;

            if [ "$#" = "0" ]
            then
                echo "The date is missed."
                show_usage="y";
                break;
            fi;

            end_coll_date=$1;
        ;;

        "-d" | "--divide-up-down")
            divide_up_down="y";
        ;;

        "-x" | "--exclude-others-flows")
            exclude_others_ports="y";
        ;;

        "-n" | "--profile-name")
            shift;

            if [ "$#" = "0" ]
            then
                echo "The profile name is missed."
                show_usage="y";
                break;
            fi;

            profile_name=$1;
        ;;

        "-m" | "--non-shadow-profile")
            shadow=0;
        ;;

        "-p" | "--ports-description")
            shift;

            if [ "$#" = "0" ]
            then
                echo "The file name is missed."
                show_usage="y";
                break;
            fi;

            ports_desc=$1;
        ;;

        "-s" | "--script")
            write_script="y";

            if [ "$#" != "1" ]
            then
                shift;
                script_file=$1;
                echo "#!/bin/bash" >  $script_file;
                echo "#"          >> $script_file;
                chmod ugo+x $script_file;
            fi;
        ;;

        *)
            echo "Unknown option \"$1\"";
            show_usage="y";
            break;
        ;;
    esac;

    shift;
done;

#
# Show usage
if [ "$show_usage" = "y" ]
then
cat << USAGE_EOF
Usage: $self [OPTIONS]...
  Options:
    -h, --help                                  print this help, then exit
    -c, --consumers N                           specify number of top consumers,
                                                N must be less or equal to $MAX_CONSUMERS,
                                                default $MAX_CONSUMERS
    -b, --begin-collecting YYYY-MM-DD-HH-MI     specify begin collecting date,
                                                default last midnight
    -e, --end-collecting YYYY-MM-DD-HH-MI       specify end collecting date,
                                                default unspecified (continuous)
    -d, --divide-up-down                        divide uplaod and download flows
                                                characteristics, default flows
                                                characteristics are aggregated
    -x, --exclude-others-flows                  exclude others flows from profile
    -n, --profile-name NAME                     define profile name, by default
                                                the profile name is constructed
                                                from the others options
                                                and look like
                                                    "top_<N>_directions_[up_down]",
                                                    where:
                                                        N       - number of top consumers
                                                                  which is defined
                                                                  by -c option
                                                        up_down - suffix which is added
                                                                  when -d option
                                                                  is specified
    -m, --non-shadow-profile                    create non-shadow profile
    -p, --ports-description FILE                define ports description file,
                                                default ./ports.desc
    -s, --script [FILE]                         write script file instead of
                                                creation profile, if FILE is omitted,
                                                STDOUT will been used

  Examples:
    $self
    $self --consumers 5 --divide-up-down --profile-name top_5_directions

USAGE_EOF
exit 0
fi;

#
# Define profile name
if [ "x$profile_name" = "x" ]
then
    if [ "$divide_up_down" = "y" ]
    then
        profile_name="top_${consumers}_directions_up_down";
    else
        profile_name="top_${consumers}_directions";
    fi;
fi;

#
# Create profile
if [ "x$end_coll_date" = "x" ]
then
    perform_action "$NFSEN --add-profile $profile_name tstart=$begin_coll_date shadow=$shadow"
else
    perform_action "$NFSEN --add-profile $profile_name tstart=$begin_coll_date tend=$end_coll_date shadow=$$shadow"
fi;

#
# Analyze netflows and generate profile's chanells
consumers_plus_1=`expr $consumers + 1 2>/dev/null`;

if [ $? != 0 ]
then
    echo "Invalid number of consumers is specified: \"$consumers\"";
    exit 1;
fi;

if [ $consumers -lt 1 -o $consumers -gt $MAX_CONSUMERS ]
then
    echo "Invalid number of consumers is specified: \"$consumers\", value must be in range [1 .. $MAX_CONSUMERS]";
    exit 1;
fi;

all_ports="0";
channel_number=1;

for port in `$NFDUMP -R $VARDIR/profiles-data/live/directions -n $consumers_plus_1 -s as/bytes -o csv | grep any | $HEAD -$consumers_plus_1 | $CUT -d , -f 5`
do
    if [ $channel_number -gt $consumers ]
    then
        break;
    fi;

    if [ $port -eq 0 ]
    then
        continue;
    fi;

    all_ports="$all_ports $port";

    desc=`$GREP "^$port\s" $ports_desc | $CUT -f2`;

    if [ "x$desc" = "x" ]
    then
        desc="port_$port";
    fi;

    colour_name=`echo COLOUR_1_$channel_number`;
    main_colour=`eval echo "$"$colour_name`;

    colour_name=`echo COLOUR_2_$channel_number`;
    supl_colour=`eval echo "$"$colour_name`;

    if [ "$divide_up_down" = "y" ]
    then
        perform_action "$NFSEN --add-channel $profile_name/${desc}_down order=$channel_number sign=+ colour=$main_colour sourcelist=directions filter=\"src as $port\""
        perform_action "$NFSEN --add-channel $profile_name/${desc}_up order=$channel_number sign=- colour=$supl_colour sourcelist=directions filter=\"dst as $port\""
    else
        perform_action "$NFSEN --add-channel $profile_name/$desc order=$channel_number sign=+ colour=$main_colour sourcelist=directions filter=\"as $port\""
    fi;

    channel_number=`expr $channel_number + 1`;
done;

#
# Add others ports
if [ "$exclude_others_ports" != "y" ]
then
    colour_name=`echo COLOUR_1_$channel_number`;
    main_colour=`eval echo "$"$colour_name`;

    colour_name=`echo COLOUR_2_$channel_number`;
    supl_colour=`eval echo "$"$colour_name`;

    if [ "$divide_up_down" = "y" ]
    then
        perform_action "$NFSEN --add-channel $profile_name/others_down order=$channel_number sign=+ colour=$main_colour sourcelist=directions filter=\"not src as in [ $all_ports ]\""
        perform_action "$NFSEN --add-channel $profile_name/others_up   order=$channel_number sign=- colour=$supl_colour sourcelist=directions filter=\"not dst as in [ $all_ports ]\""
    else
        perform_action "$NFSEN --add-channel $profile_name/others order=$channel_number sign=+ colour=$main_colour sourcelist=directions filter=\"not as in [ $all_ports ]\""
    fi;
fi;

#
# Commit changes
perform_action "$NFSEN --commit-profile $profile_name"

exit 0;