Debian Codegraph @

contrib/scripts/astversion

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/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 mainscip-shell shell shell-project 0.0.0 main().()
49 {
50 TMPFILE="/tmp/astversion.$$"
51
52 sanity_checkscip-shell shell shell-project 0.0.0 sanity_check().
53 locate_filesscip-shell shell shell-project 0.0.0 locate_files(). "$@"
54 locate_librariesscip-shell shell shell-project 0.0.0 locate_libraries().
55 locate_modulesscip-shell shell shell-project 0.0.0 locate_modules().
56
57 echoscip-shell system . . echo. "Checking Asterisk versions on $HOSTNAME at $(datescip-shell system . . date.)"
58 check_asterisk_versionscip-shell shell shell-project 0.0.0 check_asterisk_version().
59 check_dahdi_versionscip-shell shell shell-project 0.0.0 check_dahdi_version().
60
61 gather_packagesscip-shell shell shell-project 0.0.0 gather_packages().
62 if [scip-shell system . . `[`. ! -z "$DISTRO" ]
63 then
64 search_packagesscip-shell shell shell-project 0.0.0 search_packages().
65 else
66 echoscip-shell system . . echo. "WARNING: Unable to determine distro, skipping package search"
67 fi
68 search_sourcescip-shell shell shell-project 0.0.0 search_source().
69 show_unknown_filesscip-shell shell shell-project 0.0.0 show_unknown_files().
70
71 rmscip-shell system . . rm. -f $TMPFILE
72 }
73
74 sanity_checkscip-shell shell shell-project 0.0.0 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 ! whichscip-shell system . . which. $TOOL > /dev/nullscip-shell filesystem . . `/dev/null`.
83 then
84 echoscip-shell system . . echo. "ERROR: please install package for $TOOL"
85 exit 1
86 fi
87 done
88 }
89
90 locate_filesscip-shell shell shell-project 0.0.0 locate_files().()
91 {
92 # guess prefix from executable path
93 SCRIPT_PREFIX="$(readlinkscip-shell system . . readlink. -f ${0%/sbin/astversion} 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"
94 if [scip-shell system . . `[`. -x $SCRIPT_PREFIX/sbin/asterisk ]
95 then
96 PREFIX=$SCRIPT_PREFIX
97 ASTERISK_PATH=$SCRIPT_PREFIX/sbin/asterisk
98 fi
99 if [scip-shell system . . `[`. -z "$ASTERISK_PATH" ]
100 then
101 ASTERISK_PATH=$(readlinkscip-shell system . . readlink. -f $(whichscip-shell system . . which. asterisk 2>/dev/nullscip-shell filesystem . . `/dev/null`.) 2>/dev/nullscip-shell filesystem . . `/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 echoscip-shell system . . 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 echoscip-shell system . . echo. "ERROR: Unknown option: $opt"
129 exit 1
130 ;;
131 esac
132 done
133
134 # apply user supplied values
135 if [scip-shell system . . `[`. ! -z "$USER_PREFIX" ]
136 then
137 PREFIX="$USER_PREFIX"
138 ASTERISK_PATH=""
139 fi
140 if [scip-shell system . . `[`. ! -z "$USER_EXEC" ]
141 then
142 ASTERISK_PATH="$USER_EXEC"
143 fi
144
145 # locate asterisk executable
146 if [scip-shell system . . `[`. -z "$ASTERISK_PATH" ]
147 then
148 ASTERISK_PATH="$PREFIX/sbin/asterisk"
149 fi
150 if [scip-shell system . . `[`. ! -x "$ASTERISK_PATH" ]
151 then
152 echoscip-shell system . . 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=$(readlinkscip-shell system . . readlink. -f $(whichscip-shell system . . which. dahdi_cfg 2>/dev/nullscip-shell filesystem . . `/dev/null`.) 2>/dev/nullscip-shell filesystem . . `/dev/null`.)
159 if [scip-shell system . . `[`. ! -z "$DAHDI_CFG_PATH" ]
160 then
161 FILES+=($DAHDI_CFG_PATH)
162 fi
163
164 # locate asterisk libdir
165 if [scip-shell system . . `[`. -z "$LIBDIR" ]
166 then
167 LIBDIR="$PREFIX/lib"
168 if [scip-shell system . . `[`. `unamescip-shell system . . uname. -m` = "x86_64" -a -d "$PREFIX/lib64" ]
169 then
170 LIBDIR="$PREFIX/lib64"
171 fi
172 fi
173
174 if [scip-shell system . . `[`. ! -d "$LIBDIR/asterisk/modules" ]
175 then
176 echoscip-shell system . . echo. "ERROR: asterisk module directory not found at $LIBDIR"
177 exit 1
178 fi
179 }
180
181 locate_librariesscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. ! -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 [scip-shell system . . `[`. ${#FOUND_LIB[@]} -gt 1 ]
205 then
206 echoscip-shell system . . echo. "### WARNING: duplicate libraries found: ${FOUND_LIB[@]}"
207 fi
208 done
209 }
210
211 check_asterisk_versionscip-shell shell shell-project 0.0.0 check_asterisk_version().()
212 {
213 # get the version that the executable says it is
214 echoscip-shell system . . echo. "Using Asterisk executable: $ASTERISK_PATH"
215 AST_EXEC_VER=$($ASTERISK_PATH -V)
216 if [scip-shell system . . `[`. -z "$AST_EXEC_VER" ]
217 then
218 echoscip-shell system . . echo. "### ERROR: Unable to find Asterisk version from executable"
219 exit 1
220 fi
221 if [scip-shell system . . `[`. "${AST_EXEC_VER:0:9}" != "Asterisk " ]
222 then
223 echoscip-shell system . . 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/nullscip-shell filesystem . . `/dev/null`.
229 then
230 echoscip-shell system . . echo. "Installed version: $AST_EXEC_VER"
231 echoscip-shell system . . echo. "Asterisk is not running - more details are available when running."
232 AST_RUN_VER=""
233 else
234 AST_RUN_VER=$(grepscip-shell system . . grep. '^Asterisk [^e][^n][^d]' < $TMPFILE)
235 if [scip-shell system . . `[`. -z "$AST_RUN_VER" ]
236 then
237 echoscip-shell system . . 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 ! fgrepscip-shell system . . fgrep. "$AST_EXEC_VER " < $TMPFILE > /dev/nullscip-shell filesystem . . `/dev/null`.
243 then
244 echoscip-shell system . . echo. "Installed version: $AST_EXEC_VER"
245 echoscip-shell system . . echo. "### WARNING: Asterisk is running different version:"
246 fi
247 echoscip-shell system . . echo. "$AST_RUN_VER"
248 fi
249 }
250
251 check_dahdi_versionscip-shell shell shell-project 0.0.0 check_dahdi_version().()
252 {
253 if [scip-shell system . . `[`. ! -f /sys/module/dahdi/versionscip-shell filesystem . . `/sys/module/dahdi/version`. ]
254 then
255 echoscip-shell system . . echo. "Dahdi kernel module is not installed"
256 else
257 DAHDI_KERNEL=$(catscip-shell system . . cat. /sys/module/dahdi/versionscip-shell filesystem . . `/sys/module/dahdi/version`.)
258 echoscip-shell system . . echo. "Dahdi kernel module version: $DAHDI_KERNEL"
259 fi
260
261 if ! whichscip-shell system . . which. dahdi_cfg >&/dev/null
262 then
263 echoscip-shell system . . echo. "Dahdi tools are not installed"
264 else
265 DAHDI_TOOLS=$(dahdi_cfg -v |& headscip-shell system . . head. -1)
266 echoscip-shell system . . echo. "$DAHDI_TOOLS"
267 fi
268
269 if $ASTERISK_PATH -rx "dahdi show version" > $TMPFILE 2>/dev/nullscip-shell filesystem . . `/dev/null`.
270 then
271 DAHDI_CLI=$(grepscip-shell system . . grep. ^DAHDI $TMPFILE)
272 # may be empty if dahdi not installed
273 if [scip-shell system . . `[`. ! -z "$DAHDI_CLI" ]
274 then
275 echoscip-shell system . . echo. "Asterisk reports: $DAHDI_CLI"
276 else
277 echoscip-shell system . . echo. "Asterisk reports that Dahdi is not available"
278 fi
279 fi
280 }
281
282 scan_package_redhatscip-shell shell shell-project 0.0.0 scan_package_redhat().()
283 {
284 PKGNAME="$1"
285
286 if ! rpm -q $PKGNAME > /tmp/astversion-$PKGNAME-version
287 then
288 rmscip-shell system . . 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_debianscip-shell shell shell-project 0.0.0 scan_package_debian().()
298 {
299 PKGNAME="$1"
300
301 if ! dpkgscip-shell system . . dpkg. -s $PKGNAME > $TMPFILE
302 then
303 rmscip-shell system . . rm. -f /tmp/astversion-$PKGNAME-version
304 return 2
305 fi
306
307 # prefix the version with the package name to mimic rpm
308 echoscip-shell system . . echo. -n "$PKGNAME " > /tmp/astversion-$PKGNAME-version
309 catscip-shell system . . cat. $TMPFILE | fgrepscip-shell system . . fgrep. Version |cutscip-shell system . . cut. -d ' ' -f2 >> /tmp/astversion-$PKGNAME-version
310
311 dpkgscip-shell system . . dpkg. -L $PKGNAME > /tmp/astversion-$PKGNAME-files
312 dpkgscip-shell system . . dpkg. -V $PKGNAME > /tmp/astversion-$PKGNAME-verify
313 }
314
315 package_has_filescip-shell shell shell-project 0.0.0 package_has_file().()
316 {
317 PKGNAME="$1"
318 PKGFILE="$2"
319
320 if [scip-shell system . . `[`. ! -f /tmp/astversion-$PKGNAME-version ]
321 then
322 return 1
323 fi
324
325 if [scip-shell system . . `[`. ! -f /tmp/astversion-$PKGNAME-files ]
326 then
327 return 2
328 fi
329
330 if ! fgrepscip-shell system . . fgrep. "$PKGFILE" /tmp/astversion-$PKGNAME-files >/dev/nullscip-shell filesystem . . `/dev/null`.
331 then
332 # package doesn't have that file
333 return 3
334 fi
335
336 if fgrepscip-shell system . . fgrep. "$PKGFILE" /tmp/astversion-$PKGNAME-verify >/dev/nullscip-shell filesystem . . `/dev/null`.
337 then
338 # file does not match package
339 return 4
340 fi
341
342 return 0
343 }
344
345
346 gather_packagesscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -f /etc/redhat-releasescip-shell filesystem . . `/etc/redhat-release`. ]
354 then
355 DISTRO="redhat"
356 for NAME in ${SEARCH[@]}
357 do
358 PACKAGES+=($(rpm -qa |fgrepscip-shell system . . fgrep. $NAME))
359 done
360 fi
361
362 if [scip-shell system . . `[`. -f /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]
363 then
364 DISTRO="debian"
365 for NAME in ${SEARCH[@]}
366 do
367 PACKAGES+=($(dpkgscip-shell system . . dpkg. --get-selections |cutscip-shell system . . cut. -f1 |fgrepscip-shell system . . fgrep. $NAME))
368 done
369 fi
370 }
371
372 locate_modulesscip-shell shell shell-project 0.0.0 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_packagesscip-shell shell shell-project 0.0.0 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=$(catscip-shell system . . cat. /tmp/astversion-$PKGNAME-version)
391
392 FOUND=()
393 for FILE in ${FILES[@]}
394 do
395 if package_has_filescip-shell shell shell-project 0.0.0 package_has_file(). "$PACKAGE" "$FILE"
396 then
397 FOUND+=($FILE)
398 FILES=(${FILES[@]/$FILE/})
399 fi
400 done
401
402 if [scip-shell system . . `[`. ! -z "$FOUND" ]
403 then
404 if [scip-shell system . . `[`. ${#FOUND[@]} -le $CONDENSE ]
405 then
406 for FILEFOUND in ${FOUND[@]}
407 do
408 echoscip-shell system . . echo. "Matched $FILEFOUND to package $PKGVERSION"
409 done
410 else
411 echoscip-shell system . . echo. "Matched ${#FOUND[@]} files to package $PKGVERSION"
412 fi
413 fi
414
415 rmscip-shell system . . rm. -f /tmp/astversion-$PKGNAME-version
416 rmscip-shell system . . rm. -f /tmp/astversion-$PKGNAME-files
417 rmscip-shell system . . rm. -f /tmp/astversion-$PKGNAME-verify
418 done
419 }
420
421 search_sourcescip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -z "$FILES" ]
427 then
428 return
429 fi
430
431 # skip this check when without readelf tool (fedora 22)
432 if ! whichscip-shell system . . which. readelf >& /dev/null
433 then
434 echoscip-shell system . . 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=$(readelfscip-shell system . . readelf. -wi $FILE |fgrepscip-shell system . . fgrep. DW_AT_comp_dir |headscip-shell system . . head. -1)
443 COMP_DIR=${DEBUG_ELF##* }
444 DIR=${COMP_DIR//[[:space:]]/}
445 if [scip-shell system . . `[`. -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=$(basenamescip-shell system . . basename. $FILE)
470 BINARY_PATH="$DIR/$BINARY_FILE"
471 if [scip-shell system . . `[`. ! -f "$BINARY_PATH" ]
472 then
473 # it may be hiding somewhere
474 FIND_BINARY=$(findscip-shell system . . find. $DIR -name $BINARY_FILE |headscip-shell system . . head. -1)
475 if [scip-shell system . . `[`. ! -z "$FIND_BINARY" ]
476 then
477 BINARY_PATH=$FIND_BINARY
478 fi
479 fi
480 if [scip-shell system . . `[`. -f "$BINARY_PATH" ]
481 then
482 if cmpscip-shell system . . cmp. $BINARY_PATH $FILE >/dev/nullscip-shell filesystem . . `/dev/null`.
483 then
484 FOUND+=($FILE)
485 FILES=(${FILES[@]/$FILE/})
486 fi
487 fi
488 done
489
490 if [scip-shell system . . `[`. ! -z "$FOUND" ]
491 then
492 if [scip-shell system . . `[`. ${#FOUND[@]} -le $CONDENSE ]
493 then
494 for FILEFOUND in ${FOUND[@]}
495 do
496 echoscip-shell system . . echo. "Located $FILEFOUND compiled from $DIR"
497 done
498 else
499 echoscip-shell system . . echo. "Located ${#FOUND[@]} files compiled from $DIR"
500 fi
501 fi
502 done
503 }
504
505 show_unknown_filesscip-shell shell shell-project 0.0.0 show_unknown_files().()
506 {
507 # show a warning for any remaining files unaccounted for
508 if [scip-shell system . . `[`. -z "$FILES" ]
509 then
510 echoscip-shell system . . echo. "Success: all files accounted for."
511 else
512 echoscip-shell system . . echo. ""
513 echoscip-shell system . . echo. "WARNING: source of the following files was not found:"
514 if ! whichscip-shell system . . which. readelf >& /dev/null
515 then
516 for FILE in ${FILES[@]}
517 do
518 echoscip-shell system . . echo. " ### $FILE"
519 done
520 else
521 for FILE in ${FILES[@]}
522 do
523 DEBUG_ELF=$(readelfscip-shell system . . readelf. -wi $FILE |fgrepscip-shell system . . fgrep. DW_AT_comp_dir |headscip-shell system . . head. -1)
524 if [scip-shell system . . `[`. -z "$DEBUG_ELF" ]
525 then
526 COMP_DIR="(no debug info)"
527 else
528 COMP_DIR=${DEBUG_ELF##* }
529 fi
530 echoscip-shell system . . echo. " ### $FILE - $COMP_DIR"
531 done
532 fi
533 fi
534 }
535
536 mainscip-shell shell shell-project 0.0.0 main(). "$@"