From 95796e4f426d0e201dbf4365db24129d289d0694 Mon Sep 17 00:00:00 2001 From: Maxim Pogozhiy Date: Thu, 10 Feb 2022 16:14:00 +0700 Subject: [PATCH] fix exporter for version 2,3,4 --- docker-compose-2.yml | 20 ++++ docker-compose-3.4.yml | 2 +- main.go | 17 +++- tests/sphinx-2/Dockerfile | 42 ++++++++ tests/sphinx-2/docs.xml | 22 +++++ tests/sphinx-2/entrypoint.sh | 130 +++++++++++++++++++++++++ tests/sphinx-2/sphinx.conf | 39 ++++++++ tests/{sphinx => sphinx-3}/Dockerfile | 2 + tests/{sphinx => sphinx-3}/docs.xml | 0 tests/{sphinx => sphinx-3}/run.sh | 0 tests/{sphinx => sphinx-3}/sphinx.conf | 0 11 files changed, 269 insertions(+), 5 deletions(-) create mode 100644 docker-compose-2.yml create mode 100644 tests/sphinx-2/Dockerfile create mode 100644 tests/sphinx-2/docs.xml create mode 100644 tests/sphinx-2/entrypoint.sh create mode 100644 tests/sphinx-2/sphinx.conf rename tests/{sphinx => sphinx-3}/Dockerfile (94%) rename tests/{sphinx => sphinx-3}/docs.xml (100%) rename tests/{sphinx => sphinx-3}/run.sh (100%) rename tests/{sphinx => sphinx-3}/sphinx.conf (100%) diff --git a/docker-compose-2.yml b/docker-compose-2.yml new file mode 100644 index 0000000..683ba74 --- /dev/null +++ b/docker-compose-2.yml @@ -0,0 +1,20 @@ +version: "3.9" +services: +# exporter: +# container_name: exporter +# command: +# - "--sphinx.address=sphinx" +# - "--sphinx.port=3306" +# build: +# context: . +# dockerfile: Dockerfile.compose +# ports: +# - "9247:9247" + sphinx: + container_name: sphinx + build: + context: tests/sphinx-2 + ports: + - "3306:3306" + + diff --git a/docker-compose-3.4.yml b/docker-compose-3.4.yml index 87cdf8b..e079787 100644 --- a/docker-compose-3.4.yml +++ b/docker-compose-3.4.yml @@ -13,7 +13,7 @@ services: sphinx: container_name: sphinx build: - context: tests/sphinx + context: tests/sphinx-3 ports: - "3306:3306" diff --git a/main.go b/main.go index d0226f2..534e585 100644 --- a/main.go +++ b/main.go @@ -104,9 +104,18 @@ func NewExporter(server string, port string, timeout time.Duration) *Exporter { variables[variableName] = variableValue } - version, err := semver.NewVersion(strings.Split(variables["version"], " ")[0]) - if err != nil { - log.Fatal(err) + var version *semver.Version + + if len(variables["version"]) == 0 { + version, err = semver.NewVersion("2.0") + if err != nil { + log.Fatal(err) + } + } else { + version, err = semver.NewVersion(strings.Split(variables["version"], " ")[0]) + if err != nil { + log.Fatal(err) + } } return &Exporter{ @@ -646,7 +655,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { tid, proto, state, time, info string ) err = threads_rows.Scan( - &tid, &proto, &state, &state, &time, &info, + &tid, &proto, &state, &time, &info, ) if err != nil { log.Error(err) diff --git a/tests/sphinx-2/Dockerfile b/tests/sphinx-2/Dockerfile new file mode 100644 index 0000000..bb934df --- /dev/null +++ b/tests/sphinx-2/Dockerfile @@ -0,0 +1,42 @@ +FROM ubuntu:xenial +MAINTAINER romeOz + +ENV OS_LOCALE="en_US.UTF-8" \ + OS_LANGUAGE="en_US:en" \ + SPHINX_LOG_DIR=/var/log/sphinxsearch \ + SPHINX_CONF=/etc/sphinxsearch/sphinx.conf \ + SPHINX_RUN=/run/sphinxsearch/searchd.pid \ + SPHINX_DATA_DIR=/var/lib/sphinxsearch/data + +RUN buildDeps='software-properties-common python-software-properties' \ + && apt-get update && apt-get install -y $buildDeps locales --no-install-recommends \ + && add-apt-repository -y ppa:builds/sphinxsearch-rel22 \ + && apt-get update \ + && apt-get install -y sudo sphinxsearch \ + && mv -f /etc/sphinxsearch/sphinx.conf /etc/sphinxsearch/origin.sphinx.conf \ + && apt-get purge -y --auto-remove $buildDeps \ + && rm -rf /var/lib/apt/lists/* \ + # Forward sphinx logs to docker log collector + && ln -sf /dev/stdout ${SPHINX_LOG_DIR}/searchd.log \ + && ln -sf /dev/stdout ${SPHINX_LOG_DIR}/query.log + +# Set the locale +RUN locale-gen ${OS_LOCALE} +ENV LANG=${OS_LOCALE} \ + LANGUAGE=${OS_LANGUAGE} \ + LC_ALL=${OS_LOCALE} + +COPY ./entrypoint.sh /sbin/entrypoint.sh +RUN chmod 755 /sbin/entrypoint.sh +RUN mkdir -p /var/lib/sphinxsearch/index/ + +COPY ./sphinx.conf /etc/sphinxsearch/ + +COPY ./docs.xml /opt/ + +RUN indexer --all --config "/etc/sphinxsearch/sphinx.conf" + + +EXPOSE 3306 +VOLUME ["${SPHINX_DATA_DIR}"] +ENTRYPOINT ["/sbin/entrypoint.sh"] diff --git a/tests/sphinx-2/docs.xml b/tests/sphinx-2/docs.xml new file mode 100644 index 0000000..d65014a --- /dev/null +++ b/tests/sphinx-2/docs.xml @@ -0,0 +1,22 @@ + + + + + + + + + this is the main content entry +must be handled properly by xml parser lib]]> + 1012325463 + note how field/attr tags can be + in randomized order + some undeclared element + + + another subject + here comes another document, and i am given to understand, + that in-document field order must not matter, sir + 1012325467 + + diff --git a/tests/sphinx-2/entrypoint.sh b/tests/sphinx-2/entrypoint.sh new file mode 100644 index 0000000..dc96212 --- /dev/null +++ b/tests/sphinx-2/entrypoint.sh @@ -0,0 +1,130 @@ +#!/bin/bash +set -e + +SPHINX_MODE=${SPHINX_MODE:-} +SPHINX_BACKUP_DIR=${SPHINX_BACKUP_DIR:-"/tmp/backup"} +SPHINX_BACKUP_FILENAME=${SPHINX_BACKUP_FILENAME:-"backup.last.tar.gz"} +SPHINX_RESTORE=${SPHINX_RESTORE:-} +SPHINX_CHECK=${SPHINX_CHECK:-} +INDEX_NAME=${INDEX_NAME:-} +SPHINX_ROTATE_BACKUP=${SPHINX_ROTATE_BACKUP:-true} + +create_backup_dir() { + if [[ ! -d ${SPHINX_BACKUP_DIR}/ ]]; then + mkdir -p ${SPHINX_BACKUP_DIR}/ + fi + chmod -R 0755 ${SPHINX_BACKUP_DIR} +} + +create_data_dir() { + mkdir -p ${SPHINX_DATA_DIR} + chmod -R 0700 ${SPHINX_DATA_DIR} +} + +rotate_backup() +{ + echo "Rotate backup..." + + if [[ ${SPHINX_ROTATE_BACKUP} == true ]]; then + WEEK=$(date +"%V") + MONTH=$(date +"%b") + let "INDEX = WEEK % 5" || true + if [[ ${INDEX} == 0 ]]; then + INDEX=4 + fi + + test -e ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz && rm ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz + mv ${SPHINX_BACKUP_DIR}/backup.tar.gz ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz + echo "Create backup file: ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz" + + test -e ${SPHINX_BACKUP_DIR}/backup.${MONTH}.tar.gz && rm ${SPHINX_BACKUP_DIR}/backup.${MONTH}.tar.gz + ln ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz ${SPHINX_BACKUP_DIR}/backup.${MONTH}.tar.gz + echo "Create backup file: ${SPHINX_BACKUP_DIR}/backup.${MONTH}.tar.gz" + + test -e ${SPHINX_BACKUP_DIR}/backup.last.tar.gz && rm ${SPHINX_BACKUP_DIR}/backup.last.tar.gz + ln ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz ${SPHINX_BACKUP_DIR}/backup.last.tar.gz + echo "Create backup file: ${SPHINX_BACKUP_DIR}/backup.last.tar.gz" + else + mv ${SPHINX_BACKUP_DIR}/backup.tar.gz ${SPHINX_BACKUP_DIR}/backup.last.tar.gz + echo "Create backup file: ${SPHINX_BACKUP_DIR}/backup.last.tar.gz" + fi +} + +import_backup() +{ + echo "Import dump..." + FILE=$1 + if [[ ${FILE} == default ]]; then + FILE="${SPHINX_BACKUP_DIR}/${SPHINX_BACKUP_FILENAME}" + fi + if [[ ! -f "${FILE}" ]]; then + echo "Unknown backup: ${FILE}" + exit 1 + fi + create_data_dir + tar -C ${SPHINX_DATA_DIR} -xf ${FILE} +} + +sed -i "s~SPHINX_DATA_DIR~${SPHINX_DATA_DIR}~g" ${SPHINX_CONF} +sed -i "s~SPHINX_LOG_DIR~${SPHINX_LOG_DIR}~g" ${SPHINX_CONF} +sed -i "s~SPHINX_RUN~${SPHINX_RUN}~g" ${SPHINX_CONF} + +if [[ ${SPHINX_MODE} == indexing ]]; then + indexer --config ${SPHINX_CONF} --all +fi + +if [[ ${SPHINX_MODE} == backup ]]; then + echo "Backup..." + if [[ ! -d ${SPHINX_DATA_DIR} ]]; then + echo "No such directory: ${SPHINX_DATA_DIR}" + exit 1 + fi + create_backup_dir + cd ${SPHINX_DATA_DIR} + tar --ignore-failed-read -zcvf ${SPHINX_BACKUP_DIR}/backup.tar.gz *.sp* *.ram *.kill *.meta binlog.* + cd - + rotate_backup + exit 0 +fi + +# Restore from backup +if [[ -n ${SPHINX_RESTORE} ]]; then + import_backup ${SPHINX_RESTORE} +fi + + # Check backup +if [[ -n ${SPHINX_CHECK} ]]; then + + echo "Check backup..." + if [[ -z ${INDEX_NAME} ]]; then + echo "Unknown database. INDEX_NAME does not null" + exit 1; + fi + + if [[ ! -d ${SPHINX_DATA_DIR} || -z $(ls -A ${SPHINX_DATA_DIR}) ]]; then + import_backup ${SPHINX_CHECK} + fi + + if [[ $(indextool --config ${SPHINX_CONF} --check ${INDEX_NAME} | grep -w "check passed") ]]; then + echo "Success checking backup" + else + echo "Fail checking backup" + exit 1 + fi + + exit 0 +fi + +# allow arguments to be passed to Sphinx search +if [[ ${1:0:1} = '-' ]]; then + EXTRA_OPTS="$@" + set -- +fi + +# default behaviour is to launch Sphinx search +if [[ -z ${1} ]]; then + echo "Starting Sphinx search demon..." + exec $(which searchd) --config ${SPHINX_CONF} --nodetach ${EXTRA_OPTS} +else + exec "$@" +fi diff --git a/tests/sphinx-2/sphinx.conf b/tests/sphinx-2/sphinx.conf new file mode 100644 index 0000000..5045758 --- /dev/null +++ b/tests/sphinx-2/sphinx.conf @@ -0,0 +1,39 @@ +source xml +{ + type = xmlpipe2 + xmlpipe_fixup_utf8 = 1 + xmlpipe_command = cat /opt/docs.xml +} + +index test_index +{ + source = xml + path = /var/lib/sphinxsearch/index/test_index + + # @see http://sphinxsearch.com/docs/2.0.1/conf-blend-chars.html + blend_chars = - + + # CALL SUGGEST + min_infix_len = 3 + + # wsparcie dla polskich znaków + # @see http://sphinxsearch.com/wiki/doku.php?id=charset_tables#polish + charset_table = 0..9, A..Z->a..z, a..z, U+0143->U+0144, U+0104->U+0105, U+0106->U+0107, U+0118->U+0119, U+0141->U+0142, U+00D3->U+00F3, U+015A->U+015B, U+0179->U+017A, U+017B->U+017C, U+0105, U+0107, U+0119, U+0142, U+00F3, U+015B, U+017A, U+017C, U+0144 +} + +indexer +{ + mem_limit = 256M +} + +searchd +{ + listen = 3306:mysql41 + log = /var/log/searchd.log + query_log = /var/log/query.log + query_log_format = sphinxql + pid_file = SPHINX_RUN + + # binlogs + binlog_path = # disable logging +} diff --git a/tests/sphinx/Dockerfile b/tests/sphinx-3/Dockerfile similarity index 94% rename from tests/sphinx/Dockerfile rename to tests/sphinx-3/Dockerfile index e63462d..8cb4906 100644 --- a/tests/sphinx/Dockerfile +++ b/tests/sphinx-3/Dockerfile @@ -10,6 +10,8 @@ RUN apk add --no-cache mariadb-connector-c-dev \ postgresql-dev \ wget + http://sphinxsearch.com/downloads/sphinx-2.3.2-beta.tar.gz/thankyou.html + # set up and expose directories RUN mkdir -pv /opt/sphinx/log /opt/sphinx/index VOLUME /opt/sphinx/index diff --git a/tests/sphinx/docs.xml b/tests/sphinx-3/docs.xml similarity index 100% rename from tests/sphinx/docs.xml rename to tests/sphinx-3/docs.xml diff --git a/tests/sphinx/run.sh b/tests/sphinx-3/run.sh similarity index 100% rename from tests/sphinx/run.sh rename to tests/sphinx-3/run.sh diff --git a/tests/sphinx/sphinx.conf b/tests/sphinx-3/sphinx.conf similarity index 100% rename from tests/sphinx/sphinx.conf rename to tests/sphinx-3/sphinx.conf