Debian Codegraph @

contrib/scripts/astversion

raw symbols SCIP
1 #!/bin/bash
2 #
3 # astversion - determine version/source of components
4 #
5 # use: astverion {options}
6 #
7 # options:
8 # --prefix=PATH - specify prefix from build
9 # --exec=PATH - specify asterisk executable
10 # --lib=PATH - specify asterisk library path
11 # --src=PATH - specify source path to search
12 #
13 # Copyright (c) 2015, Digium, Inc.
14 #
15 # Written by Scott Griepentrog <[email protected]>
16 #
17 # Distributed under the terms of the GNU General Public License
18
19 # condense list of files when more than X in a set
20 CONDENSE=3
21
22 # libraries to provide the source/version of
23 LIBRARIES=(
24 libasteriskssl.so.1
25 libspandsp.so.2
26 libpjsip.so.2
27 libpri.so.1.4
28 )
29
30 # possible library locations
31 LIB_PATHS=(
32 /usr/lib
33 /usr/lib64
34 /lib
35 /lib64
36 /usr/local/lib
37 /usr/local/lib64
38 /opt/lib
39 /opt/lib64
40 )
41
42 # collection of files to search for
43 FILES=()
44
45 # source directories to search
46 SRC_DIRS=()
47
48 main()
49 {
50 TMPFILE="/tmp/astversion.$$"
51
52 sanity_check
53 locate_files "$@"
54 locate_libraries
55 locate_modules
56
57 echo "Checking Asterisk versions on $HOSTNAME at $(date)"
58 check_asterisk_version
59 check_dahdi_version
60
61 gather_packages
62 if [ ! -z "$DISTRO" ]
63 then
64 search_packages
65 else
66 echo "WARNING: Unable to determine distro, skipping package search"
67 fi
68 search_source
69 show_unknown_files
70
71 rm -f $TMPFILE
72 }
73
74 sanity_check()
75 {
76 # insure that needed tools are present
77 TOOLS=(uname basename fgrep cut head readlink find)
78 # making assumption that rpm and dpkg always exist on their platforms
79
80 for TOOL in ${TOOLS[@]}
81 do
82 if ! which $TOOL > /dev/null
83 then
84 echo "ERROR: please install package for $TOOL"
85 exit 1
86 fi
87 done
88 }
89
90 locate_files()
91 {
92 # guess prefix from executable path
93 SCRIPT_PREFIX="$(readlink -f ${0%/sbin/astversion} 2>/dev/null)"
94 if [ -x $SCRIPT_PREFIX/sbin/asterisk ]
95 then
96 PREFIX=$SCRIPT_PREFIX
97 ASTERISK_PATH=$SCRIPT_PREFIX/sbin/asterisk
98 fi
99 if [ -z "$ASTERISK_PATH" ]
100 then
101 ASTERISK_PATH=$(readlink -f $(which asterisk 2>/dev/null) 2>/dev/null)
102 PREFIX=${ASTERISK_PATH%/sbin/asterisk}
103 fi
104
105 # parse user supplied information
106 USER_PREFIX=""
107 USER_EXEC=""
108 for opt in "$@"
109 do
110 case "$opt" in
111 -h|--help)
112 echo "Use: astversion {--prefix=PATH} {--exec=PATH} {--lib=PATH}"
113 exit 0
114 ;;
115 --prefix=*)
116 USER_PREFIX=${opt:9}
117 ;;
118 --exec=*)
119 USER_EXEC=${opt:7}
120 ;;
121 --lib=*)
122 LIBDIR=${opt:6}
123 ;;
124 --src=*)
125 SRC_DIRS+=${opt:6}
126 ;;
127 *)
128 echo "ERROR: Unknown option: $opt"
129 exit 1
130 ;;
131 esac
132 done
133
134 # apply user supplied values
135 if [ ! -z "$USER_PREFIX" ]
136 then
137 PREFIX="$USER_PREFIX"
138 ASTERISK_PATH=""
139 fi
140 if [ ! -z "$USER_EXEC" ]
141 then
142 ASTERISK_PATH="$USER_EXEC"
143 fi
144
145 # locate asterisk executable
146 if [ -z "$ASTERISK_PATH" ]
147 then
148 ASTERISK_PATH="$PREFIX/sbin/asterisk"
149 fi
150 if [ ! -x "$ASTERISK_PATH" ]
151 then
152 echo "ERROR: the Asterisk executable is not found or not executable at $ASTERISK_PATH"
153 exit 1
154 fi
155 FILES+=($ASTERISK_PATH)
156
157 # locate dahdi_cfg executable
158 DAHDI_CFG_PATH=$(readlink -f $(which dahdi_cfg 2>/dev/null) 2>/dev/null)
159 if [ ! -z "$DAHDI_CFG_PATH" ]
160 then
161 FILES+=($DAHDI_CFG_PATH)
162 fi
163
164 # locate asterisk libdir
165 if [ -z "$LIBDIR" ]
166 then
167 LIBDIR="$PREFIX/lib"
168 if [ `uname -m` = "x86_64" -a -d "$PREFIX/lib64" ]
169 then
170 LIBDIR="$PREFIX/lib64"
171 fi
172 fi
173
174 if [ ! -d "$LIBDIR/asterisk/modules" ]
175 then
176 echo "ERROR: asterisk module directory not found at $LIBDIR"
177 exit 1
178 fi
179 }
180
181 locate_libraries()
182 {
183 # LIBDIR should contain libasteriskssl, but others may be elsewhere
184
185 # add LIBDIR to path list
186 if ! [[ " ${LIB_PATHS[@]} " =~ " $LIBDIR " ]]
187 then
188 LIB_PATHS+=($LIBDIR)
189 fi
190
191 for LIBRARY in ${LIBRARIES[@]}
192 do
193 FOUND_LIB=()
194 for LIB_PATH in ${LIB_PATHS[@]}
195 do
196 FULL_PATH="$LIB_PATH/$LIBRARY"
197 if [ ! -L $LIB_PATH -a -f $FULL_PATH ]
198 then
199 FOUND_LIB+=($FULL_PATH)
200 FILES+=($FULL_PATH)
201 fi
202 done
203
204 if [ ${#FOUND_LIB[@]} -gt 1 ]
205 then
206 echo "### WARNING: duplicate libraries found: ${FOUND_LIB[@]}"
207 fi
208 done
209 }
210
211 check_asterisk_version()
212 {
213 # get the version that the executable says it is
214 echo "Using Asterisk executable: $ASTERISK_PATH"
215 AST_EXEC_VER=$($ASTERISK_PATH -V)
216 if [ -z "$AST_EXEC_VER" ]
217 then
218 echo "### ERROR: Unable to find Asterisk version from executable"
219 exit 1
220 fi
221 if [ "${AST_EXEC_VER:0:9}" != "Asterisk " ]
222 then
223 echo "### ERROR: Unexpected version from executable: $AST_EXEC_VER"
224 exit 1
225 fi
226
227 # compare with the version that is running
228 if ! $ASTERISK_PATH -rx "core show version" > $TMPFILE 2>/dev/null
229 then
230 echo "Installed version: $AST_EXEC_VER"
231 echo "Asterisk is not running - more details are available when running."
232 AST_RUN_VER=""
233 else
234 AST_RUN_VER=$(grep '^Asterisk [^e][^n][^d]' < $TMPFILE)
235 if [ -z "$AST_RUN_VER" ]
236 then
237 echo "### ERROR: Unable to find Asterisk version from running instance"
238 exit 1
239 fi
240
241 # is it running the same version? (note: space is significant!)
242 if ! fgrep "$AST_EXEC_VER " < $TMPFILE > /dev/null
243 then
244 echo "Installed version: $AST_EXEC_VER"
245 echo "### WARNING: Asterisk is running different version:"
246 fi
247 echo "$AST_RUN_VER"
248 fi
249 }
250
251 check_dahdi_version()
252 {
253 if [ ! -f /sys/module/dahdi/version ]
254 then
255 echo "Dahdi kernel module is not installed"
256 else
257 DAHDI_KERNEL=$(cat /sys/module/dahdi/version)
258 echo "Dahdi kernel module version: $DAHDI_KERNEL"
259 fi
260
261 if ! which dahdi_cfg >&/dev/null
262 then
263 echo "Dahdi tools are not installed"
264 else
265 DAHDI_TOOLS=$(dahdi_cfg -v |& head -1)
266 echo "$DAHDI_TOOLS"
267 fi
268
269 if $ASTERISK_PATH -rx "dahdi show version" > $TMPFILE 2>/dev/null
270 then
271 DAHDI_CLI=$(grep ^DAHDI $TMPFILE)
272 # may be empty if dahdi not installed
273 if [ ! -z "$DAHDI_CLI" ]
274 then
275 echo "Asterisk reports: $DAHDI_CLI"
276 else
277 echo "Asterisk reports that Dahdi is not available"
278 fi
279 fi
280 }
281
282 scan_package_redhat()
283 {
284 PKGNAME="$1"
285
286 if ! rpm -q $PKGNAME > /tmp/astversion-$PKGNAME-version
287 then
288 rm -f /tmp/astversion-$PKGNAME-version
289 return 2
290 fi
291
292 rpm -ql $PKGNAME > /tmp/astversion-$PKGNAME-files
293 rpm -V $PKGNAME > /tmp/astversion-$PKGNAME-verify
294 return 0
295 }
296
297 scan_package_debian()
298 {
299 PKGNAME="$1"
300
301 if ! dpkg -s $PKGNAME > $TMPFILE
302 then
303 rm -f /tmp/astversion-$PKGNAME-version
304 return 2
305 fi
306
307 # prefix the version with the package name to mimic rpm
308 echo -n "$PKGNAME " > /tmp/astversion-$PKGNAME-version
309 cat $TMPFILE | fgrep Version |cut -d ' ' -f2 >> /tmp/astversion-$PKGNAME-version
310
311 dpkg -L $PKGNAME > /tmp/astversion-$PKGNAME-files
312 dpkg -V $PKGNAME > /tmp/astversion-$PKGNAME-verify
313 }
314
315 package_has_file()
316 {
317 PKGNAME="$1"
318 PKGFILE="$2"
319
320 if [ ! -f /tmp/astversion-$PKGNAME-version ]
321 then
322 return 1
323 fi
324
325 if [ ! -f /tmp/astversion-$PKGNAME-files ]
326 then
327 return 2
328 fi
329
330 if ! fgrep "$PKGFILE" /tmp/astversion-$PKGNAME-files >/dev/null
331 then
332 # package doesn't have that file
333 return 3
334 fi
335
336 if fgrep "$PKGFILE" /tmp/astversion-$PKGNAME-verify >/dev/null
337 then
338 # file does not match package
339 return 4
340 fi
341
342 return 0
343 }
344
345
346 gather_packages()
347 {
348 # build a list of installed packages that are likely to contain files of interest
349 PACKAGES=()
350 SEARCH=(asterisk dahdi libpri pjproject spandsp)
351 DISTRO=""
352
353 if [ -f /etc/redhat-release ]
354 then
355 DISTRO="redhat"
356 for NAME in ${SEARCH[@]}
357 do
358 PACKAGES+=($(rpm -qa |fgrep $NAME))
359 done
360 fi
361
362 if [ -f /etc/debian_version ]
363 then
364 DISTRO="debian"
365 for NAME in ${SEARCH[@]}
366 do
367 PACKAGES+=($(dpkg --get-selections |cut -f1 |fgrep $NAME))
368 done
369 fi
370 }
371
372 locate_modules()
373 {
374 # build a list of files that need to be located
375 MODULES=($LIBDIR/asterisk/modules/*.so)
376
377 # add libraries and binaries that exist to the files list
378 for MODULE in ${MODULES[@]}
379 do
380 FILES+=($MODULE)
381 done
382 }
383
384 search_packages()
385 {
386 # search each package and report files that match
387 for PACKAGE in ${PACKAGES[@]}
388 do
389 scan_package_$DISTRO "$PACKAGE"
390 PKGVERSION=$(cat /tmp/astversion-$PKGNAME-version)
391
392 FOUND=()
393 for FILE in ${FILES[@]}
394 do
395 if package_has_file "$PACKAGE" "$FILE"
396 then
397 FOUND+=($FILE)
398 FILES=(${FILES[@]/$FILE/})
399 fi
400 done
401
402 if [ ! -z "$FOUND" ]
403 then
404 if [ ${#FOUND[@]} -le $CONDENSE ]
405 then
406 for FILEFOUND in ${FOUND[@]}
407 do
408 echo "Matched $FILEFOUND to package $PKGVERSION"
409 done
410 else
411 echo "Matched ${#FOUND[@]} files to package $PKGVERSION"
412 fi
413 fi
414
415 rm -f /tmp/astversion-$PKGNAME-version
416 rm -f /tmp/astversion-$PKGNAME-files
417 rm -f /tmp/astversion-$PKGNAME-verify
418 done
419 }
420
421 search_source()
422 {
423 # look for source path locally (compiled on this machine)
424 # - scan elfs for compilation directory
425 # - compare the file to confirm match
426 if [ -z "$FILES" ]
427 then
428 return
429 fi
430
431 # skip this check when without readelf tool (fedora 22)
432 if ! which readelf >& /dev/null
433 then
434 echo "Warning: skipping source detection because readelf utility is not available"
435 return
436 fi
437
438 # build a list of source paths
439 DIRS=()
440 for FILE in ${FILES[@]}
441 do
442 DEBUG_ELF=$(readelf -wi $FILE |fgrep DW_AT_comp_dir |head -1)
443 COMP_DIR=${DEBUG_ELF##* }
444 DIR=${COMP_DIR//[[:space:]]/}
445 if [ -d $DIR ]
446 then
447 if ! [[ " ${DIRS[@]} " =~ " $DIR " ]]
448 then
449 DIRS+=($DIR)
450 fi
451 fi
452 done
453
454 # add in user specified directories last
455 for DIR in ${SRC_DIRS[@]}
456 do
457 if ! [[ " ${DIRS[@]} " =~ " $DIR " ]]
458 then
459 DIRS+=($DIR)
460 fi
461 done
462
463 # for each source path, look for target file
464 for DIR in ${DIRS[@]}
465 do
466 FOUND=()
467 for FILE in ${FILES[@]}
468 do
469 BINARY_FILE=$(basename $FILE)
470 BINARY_PATH="$DIR/$BINARY_FILE"
471 if [ ! -f "$BINARY_PATH" ]
472 then
473 # it may be hiding somewhere
474 FIND_BINARY=$(find $DIR -name $BINARY_FILE |head -1)
475 if [ ! -z "$FIND_BINARY" ]
476 then
477 BINARY_PATH=$FIND_BINARY
478 fi
479 fi
480 if [ -f "$BINARY_PATH" ]
481 then
482 if cmp $BINARY_PATH $FILE >/dev/null
483 then
484 FOUND+=($FILE)
485 FILES=(${FILES[@]/$FILE/})
486 fi
487 fi
488 done
489
490 if [ ! -z "$FOUND" ]
491 then
492 if [ ${#FOUND[@]} -le $CONDENSE ]
493 then
494 for FILEFOUND in ${FOUND[@]}
495 do
496 echo "Located $FILEFOUND compiled from $DIR"
497 done
498 else
499 echo "Located ${#FOUND[@]} files compiled from $DIR"
500 fi
501 fi
502 done
503 }
504
505 show_unknown_files()
506 {
507 # show a warning for any remaining files unaccounted for
508 if [ -z "$FILES" ]
509 then
510 echo "Success: all files accounted for."
511 else
512 echo ""
513 echo "WARNING: source of the following files was not found:"
514 if ! which readelf >& /dev/null
515 then
516 for FILE in ${FILES[@]}
517 do
518 echo " ### $FILE"
519 done
520 else
521 for FILE in ${FILES[@]}
522 do
523 DEBUG_ELF=$(readelf -wi $FILE |fgrep DW_AT_comp_dir |head -1)
524 if [ -z "$DEBUG_ELF" ]
525 then
526 COMP_DIR="(no debug info)"
527 else
528 COMP_DIR=${DEBUG_ELF##* }
529 fi
530 echo " ### $FILE - $COMP_DIR"
531 done
532 fi
533 fi
534 }
535
536 main "$@"